0

I would like to aggregate over multiple LHS variables in R, with the outcome being distinct columns for each aggregation.

df <- data.frame(time = rep(seq(1,2,1),2), 
                 v1 = seq(2,8,2), 
                 v2 = seq(2,16,4),
                 f = c("a","b","c","d"))  

df$f <- as.character(df$f)
aggregate(df$v1 + df$v2 ~ df$time, FUN=sum) 

EDIT: Previous answers have suggested

aggregate(. ~ time, df, sum)

Which works as along as there are only numeric variables that are being aggregated. I have updated the code to reflect a situation in which a character type is present. I am working with a large data frame that has character variables. It is obviously possible to first extract the numeric variables from the data frame, but is there a way to do this directly? Thanks.

Bjørn Kallerud
  • 979
  • 8
  • 23
  • 2
    I guess you want this: `aggregate(. ~ time, df, sum)` – pogibas Jan 09 '19 at 16:38
  • 1
    I guess you are looking for `library(dplyr); df %>% group_by(time) %>% summarise_all(sum)` – akrun Jan 09 '19 at 16:38
  • @PoGibas I have updated my question to reflect a situation in which there are not only numeric variables, which is at the root of my problem. – Bjørn Kallerud Jan 09 '19 at 16:53
  • 1
    Here's a quick solution to subset numeric columns: `aggregate(. ~ time, df[, c(1, which(sapply(df[, -1], class) == "numeric") + 1)], sum)` – pogibas Jan 09 '19 at 16:58

0 Answers0