How do you abstract aggregate
in a function by passing a list of conditions and values to summarize?
# This works fine:
x <- data.frame(cond1 = sample(letters[1:3], 500, replace=TRUE),
cond2 = sample(LETTERS[1:7], 500, replace = TRUE),
cond3 = sample(LETTERS[1:4], 500, replace = TRUE),
value1 = rnorm(500),
value2 = rnorm(500))
aggregate(cbind(value1,value2) ~ cond1 + cond2, data = x, FUN=sum)
Need to create a list of column names: (3 options shown) then call the function:
c1 <- c("cond1","cond2","cond3"); v1 <- c("value1","value2")
c1 <- c("cond2","cond3"); v1 <- c("value2")
c1 <- c("cond3"); v1 <- c("value1")
aggregate(cbind(v1) ~ c1, data = x, FUN=sum)
I have reviewed many alternatives, but have not yet discovered the key to this abstraction.
You can use the alternative interface to
aggregate
, which does not use a formula:Result is a list of three dataframes