Skip to content

[R-Forge #5732] Failure to evaluate parsed expression in list in j (not using :=) #472

@arunsrinivasan

Description

@arunsrinivasan

Submitted by: Benjamin Barnes; Assigned to: Nobody; R-Forge link

library(data.table)
## data.table 1.9.3  For help type: help("data.table")

set.seed(100)
nrow <- 100
DT <- data.table(aa = sample(letters[1:5], nrow, replace = TRUE), bb = rnorm(nrow))
sumExpr <- parse(text = "sum(bb, na.rm = TRUE)")
meanExpr <- parse(text = "mean(bb, na.rm = TRUE)")

## These work
DT[, eval(sumExpr), by = aa]
DT[, eval(meanExpr), by = aa]

## This does not work
DT[, list(mySum = eval(sumExpr), myMean = eval(meanExpr)), by = aa]
## Error in `[.data.table`(DT, , list(mySum = eval(sumExpr), myMean = eval(meanExpr)),  : 
##   Unsupported type 'expression'

## This works
DT[, {
    out <- list(mySum = eval(sumExpr), myMean = eval(meanExpr))
    lapply(out, eval, .SD)
    }, by = aa]

Perhaps there has been a change of paradigm concerning evaluation of expressions in data.tables - if so, please excuse my ignorance!

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions