I'm having a lot of trouble trying to figure out how to use the aggregate function in R and I'd appreciate some guidance. About the only way I could get aggregate to work was by following the syntax given here.
Given the data
data <- data.frame(alpha = c(1,1,1,1,2,2,2,2),
beta = c(1,1,2,2,3,3,4,4),
gamma = c(.1,.2,.3,.2,.4,.1,.3,.5))
I would like to aggregate by the columns 'alpha'
and 'beta'
. If I was just aggregating by 'alpha'
, for example, I could just do:
aggregate(x = data[ , colnames(data) != "alpha"],
by = list(data$alpha),
FUN = sum)
Trying
aggregate(x = data[ , colnames(data) != "alpha" | colnames(data) != "beta"],
by = list(data$alpha, data$beta),
FUN = sum)
didn't work. Could someone help me find the correct syntax?
Thanks!
CodePudding user response:
Using the formula interface might be easier here:
aggregate(gamma ~ alpha beta, data = data, FUN = sum)
CodePudding user response:
how about:
library(dplyr)
data.frame(alpha = c(1,1,1,1,2,2,2,2),
beta = c(1,1,2,2,3,3,4,4),
gamma = c(.1,.2,.3,.2,.4,.1,.3,.5)) %>%
group_by(alpha) %>%
summarise(across(.fns = sum))
# A tibble: 2 x 3
alpha beta gamma
<dbl> <dbl> <dbl>
1 1 6 0.8
2 2 14 1.3
?
CodePudding user response:
Using data.table
library(data.table)
setDT(data)[, .(gamma = sum(gamma)), .(alpha, beta)]