How to use aggregates with list names

How do I summarize the aggregation in a function by passing a list of conditions and values?

# 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: (display 3 options) and 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 found the key to this abstraction.

You can use alternative interfaces for Aggregate, but don’t use the formula:

c1 <- c("cond1","cond2","cond3")
v1 <- c( "value1","value2")
aggregate(x[v1],by=x[c1],FUN=sum)

cond1 cond2 cond3 va lue1 value2
1 a AA -3.3025839 -0.98304649
2 b AA 0.6326985 -3.08677485
3 c AA 3.6007853 2.23962265
4 a BA -0.5247620 -0.94644740
5 b BA 0.9242562 2.48268452
6 c BA 6.9215712 0.31512645

How to summarize the aggregation in a function by passing a list of conditions and values?

# 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: (display 3 options) and 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 found the key to this abstraction.

You can use alternative interfaces for aggregation, but don’t use the formula:

c1 <- c("cond1","cond2","cond3")
v1 <- c("value1","value2")
aggregate(x[v1],by=x[c1],FUN=sum)

cond1 cond2 cond3 value1 value2
1 a AA -3.3025839 -0.98 304649
2 b AA 0.6326985 -3.08677485
3 c AA 3.6007853 2.23962265
4 a BA -0.5247620 -0.94644740
5 b BA 0.9242562 2.48268452
6 c BA 6.9215712 0.31512645< /pre>

Leave a Comment

Your email address will not be published.