fzenoni
fzenoni

Reputation: 128

Group by a variable number of columns in R data.table

Consider the following:

library(data.table)
dt <- data.table(CO2)

What if I wanted to conditionally do:

dt[, mean(conc), by = .(Type, round(uptake))]

OR

dt[, mean(conc), by = round(uptake)]

depending on the value of some other boolean variable bool? I'd just like to avoid repeating two very similar commands in an if else form and I'm wondering if it's possible at all with data.table.

I tried the following:

bool <- TRUE
dt[, mean(conc), by = .(unlist(ifelse(bool, list(Type), list(NULL))), round(uptake))]

which works in this case, but if bool <- FALSE, it gives this error:

Error in `[.data.table`(dt, , mean(conc), by = .(unlist(ifelse(FALSE,  : 
  column or expression 1 of 'by' or 'keyby' is type NULL. Do not quote column names. Usage: DT[,sum(colC),by=list(colA,month(colB))]

Upvotes: 0

Views: 84

Answers (1)

s_baldur
s_baldur

Reputation: 33498

A quick something:

gby <- c('Type', 'tmp')[c(bool, TRUE)]
dt[, tmp := round(uptake)][, mean(conc), by = gby]

Upvotes: 1

Related Questions