从data.table获取字符串列

the*_*ega 26 r data.table

rawdata.table和以下代码一起工作:

raw[,r_responseTime] #Returns the whole column
raw[,c_filesetSize]  #Same as above, returns column
plot(raw[,r_responseTime]~raw[,c_filesetSize]) #draws something
Run Code Online (Sandbox Code Playgroud)

现在我想从字符串中指定这些列,例如:

col1="r_reponseTime"
col2="c_filesetSize"
Run Code Online (Sandbox Code Playgroud)

现在,如何通过字符串引用列来实现与上面相同的功能?

raw[,col1] #Returns the whole column
raw[,col2]  #Same as above, returns column
plot(raw[,col1]~raw[,col2]) #draws something
Run Code Online (Sandbox Code Playgroud)

不起作用,当然因为我需要某种"解除歧视".我不知道在帮助和互联网上搜索什么,很抱歉这个愚蠢的问题.

flo*_*del 32

如果您提供了一个可重现的示例,或者至少显示了列的名称raw是什么r_responseTime以及c_filesetSize包含什么,那就太好了.这就是说,get你的解除引用功能,所以尝试一下:

raw[, get(col1)]
raw[, get(col2)]
plot(raw[, get(col1)] ~ raw[, get(col2)])
Run Code Online (Sandbox Code Playgroud)


Eti*_*rie 8

另一种方法是使用..函数或..运算符.

raw[ , ..col1]
Run Code Online (Sandbox Code Playgroud)

  • 比赛很好.我需要使用`with = FALSE`,如:`raw [,match(col1,names(raw)),with = FALSE]` (2认同)

tad*_*jsv 6

如果您有字符串向量,则可以使用 mget

cols = c("r_reponseTime", "c_filesetSize")
raw[, mget(cols)]
Run Code Online (Sandbox Code Playgroud)