0

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!

Red Sleuth
  • 111
  • 3
  • What is your expected result? I.e. do you want to get the sum of gamma by all possible combinations of alpha and beta pr do you want to get the sum of gamma by aggregating alpha and then also get the sum of gamma when aggregating beta? – deschen Sep 21 '21 at 22:28

2 Answers2

1

Using the formula interface might be easier here:

aggregate(gamma ~ alpha + beta, data = data, FUN = sum)
Noah
  • 3,437
  • 1
  • 11
  • 27
0

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

?

huttoncp
  • 161
  • 4