0

So a pretty common pattern is this:

dt[, lapply(.SD, sum, na.rm=TRUE), by=category]

which will sum a subset grouped by the category.

But what if I want to sum col a but mean col b? (using the .SD)

wizard_draziw
  • 505
  • 1
  • 5
  • 17

1 Answers1

0

Assuming that you have a pattern in column names, I'll use iris dataset as an example, and the pattern will be the word 'Width'.

dt=as.data.table(iris)
dt[,
   lapply(.SD,
          function(X)
            ifelse(grepl("Width",deparse(substitute(X))),sum(X),mean(X))
          ),by=Species]