I'm aggregating multiple columns in an R data frame using the following method:
data = data.frame(X=c(1, 3, 3), Y=c(2, 3, 3), Z=c(5, 6, 7), ID=c(10, 11, 12))
with(data, aggregate(cbind(Z, ID), list(X=X, Y=Y), c))
However, I want to select columns using a list variable. I've tried some intricate paste
statements looking at this question and this question, but I get error messages like "object 'Z' not found".
How do I do this in a short-hand, easy-to-read manner using base R, preferably without libraries, do.call
or hard-to-read paste
statements full of parenthesis and apostrophes? Ideally:
k = c("Z", "ID")
aggregate(magic(k), list(X=X, Y=Y), data, c)