Both subset
and with
are designed for interactive use and warnings against their use within other functions will be found in their help pages. This stems from their strategy of evaluation arguments as expressions within an environment constructed from the names of their data arguments. These column/element names would otherwise not be "objects" in the R-sense.
If THECOLUMN
is the name of an object whose value is the name of the column and someValue
is the name of an object whose value is the target, then you should use:
dfrm[ dfrm[[THECOLUMN]] == someValue , ]
The fact that "[[" will evaluate its argument is why it is superior to "$" for programing. If we use joran's example:
d <- data.frame(x = letters[1:5],y = runif(5))
THECOLUMN= "x"
someValue= "c"
d[ d[[THECOLUMN]] == someValue , ]
# x y
# 3 c 0.7556127
So in this case all these return the same atomic vector:
d[[ THECOLUMN ]]
d[[ 'x' ]]
d[ , 'x' ]
d[, THECOLUMN ]
d$x # of the three extraction functions: `$`, `[[`, and `[`,
# only `$` is unable to evaluate its argument
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…