0

I want to create a new dataframe with the sums of multiple variables by two grouping variables.

My data is organised as below

Management    Habitat      Var1    Var2   Var3 
   A           Urban        12       6     18
   A          Farmland       1       9     10
   A          Farmland       3      10     17
   B          Forest         5      17     12
   B          Peatland      18      23     18
   C          Peatland      16      22     20
   C          Urban         16      10     21

I have the code for summing one variable across management and habitat, however I can't sum multiple variables across management and habitat to creat a new dataframe.

Code for summing one variable

newdf<-aggregate(df$`Var1` , by=list(Category=df$Management, Category=df$Habitat), FUN=sum)

1 Answers1

0

You can use . ~ Management + Habitat in aggregate to sum all remaining columns:

aggregate(. ~ Management + Habitat, x, sum)
#  Management  Habitat Var1 Var2 Var3
#1          A Farmland    4   19   27
#2          B   Forest    5   17   12
#3          B Peatland   18   23   18
#4          C Peatland   16   22   20
#5          A    Urban   12    6   18
#6          C    Urban   16   10   21

Data:

x <- structure(list(Management = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 
3L), .Label = c("A", "B", "C"), class = "factor"), Habitat = structure(c(4L, 
1L, 1L, 2L, 3L, 3L, 4L), .Label = c("Farmland", "Forest", "Peatland", 
"Urban"), class = "factor"), Var1 = c(12L, 1L, 3L, 5L, 18L, 16L, 
16L), Var2 = c(6L, 9L, 10L, 17L, 23L, 22L, 10L), Var3 = c(18L, 
10L, 17L, 12L, 18L, 20L, 21L)), class = "data.frame", row.names = c(NA, 
-7L))
GKi
  • 37,245
  • 2
  • 26
  • 48