Skip to content

[R-Forge #5527] Different scoping rule when assigning by reference a new column #30

Closed
@arunsrinivasan

Description

Submitted by: Michele Carriero; Assigned to: Arun ; R-Forge link

Given the following table dt:

dt<-data.table(id=1:5, var=letters[1:5])

If I need to retrieve programmatically a column I use eval(parse(text=variable)). But when the variable, containing (part of) the column name to retrieve, is also named alike another column in the table I have two different outcomes:

id<-"va"
dt[, eval(parse(text=paste0(id,"r")))]
# [1] "a" "b" "c" "d" "e"

dt[, id2:=eval(parse(text=paste0(id,"r")))]
# Error in parse(text = paste0(id, "r")) : <text>:1:2: unexpected symbol
1: 1r
    ^

I thought that the above was not intended to be so. I would expect the same result (probably the second).

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions