Select / assign to data.table when variable names are stored in a character vector

后端 未结 5 1426
無奈伤痛
無奈伤痛 2020-11-22 03:54

How do you refer to variables in a data.table if the variable names are stored in a character vector? For instance, this works for a data.frame:

5条回答
  •  野性不改
    2020-11-22 04:27

    You could try this

    colname <- as.name("COL_NAME")

    DT2 <- DT[, list(COL_SUM=sum(eval(colname, .SD))), by = c(group)]

提交回复
热议问题