How to aggregate multiple columns ?

--Hi,

i want to aggregate 2 columns in the same time by summing values, for example:

# creating data frame 
df <- data.frame(Sub = c('Math', 'Math', 'Phy', 'Phy', 
						'Phy', 'Che', 'Che'), 
				Marks = c(8, 2, 4, 9, 9, 7, 1), 
				Add_on = c(3, 1, 9, 4, 7, 8, 2)) 

# view dataframe 
df
Sub    Marks    Add_on
Math    8    3
Math    2    1
Phy    4    9
Phy    9    4
Phy    9    7
Che    7    8
Che    1    2

aggregate(df$Marks, list(df$Sub), FUN=sum) 
aggregate(df$Add_on, list(df$Sub), FUN=sum)

Group.1    x
Che    8
Math    10
Phy    22

Group.1    x
Che    10
Math    4
Phy    20

i have used aggregate 2 times, is there a way to sum the 2 columns using only 1 command ?
to obtain that:

Sub x y
Che 8 10
Math 10 4
Phy 22 20

thank you --

Using base R:

aggregate(x = df[c("Marks", "Add_on")], by = df["Sub"], FUN = sum)

dplyr without needing to specify the numeric columns:

library(dplyr)
df %>% 
  group_by(Sub) %>% 
  summarise(across(where(is.numeric), ~sum(.x)))

data.table without needing to specify the numeric columns:

library(data.table)
dt <- setDT(df) # create a separate data.table
dt[, lapply(.SD, sum), .SDcols = is.numeric, by = Sub]

In R there are probably several ways. I prefer to use {data.table}

library(data.table)
dt <- data.frame(Sub = c('Math', 'Math', 'Phy', 'Phy', 
						'Phy', 'Che', 'Che'), 
				Marks = c(8, 2, 4, 9, 9, 7, 1), 
				Add_on = c(3, 1, 9, 4, 7, 8, 2)) 

DT <-  as.data.table(dt)

DT[, .(sumM = sum(Marks),  sumA  = sd(Add_on)), by = Sub]

It occurs to me that this modification to aggregate should be fine.

aggregate(dt[, 2:3], by = list(dt$Sub), sum)

Hi all,

i have used Martin.R soluce
it works fine, thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.