[英]How to filter a data.table based on values of Column where Column name is stored in a variable
Let say I have a data.table
as below -假设我有一个
data.table
如下 -
DT = data.table('Col1' = c(NA, 100), 'Col2' = letters[1:2])
Now I want to filter this DT
for which Col1
is NA
.现在我想过滤
Col1
为NA
的这个DT
。 However Col1
is stored in a variable -但是
Col1
存储在一个变量中 -
Col_Name = 'Col1'
DT[is.na(Col_Name)]
### Empty data.table (0 rows and 2 cols): Col1,Col2
Any idea why I am getting 0 row?知道为什么我得到 0 行吗?
However if I do但是,如果我这样做
DT[is.na(Col1)]
I am getting correct result.我得到了正确的结果。
What is the wrong with first approach?第一种方法有什么问题?
Any pointer will be highly appreciated.任何指针将不胜感激。
Thanks,谢谢,
You are in a typical example of standard evaluation problem.您是标准评估问题的典型示例。 With
data.table
it's quite easy to handle variable names.使用
data.table
处理变量名非常容易。 In your case, get
does perfectly the job在您的情况下,
get
可以完美地完成工作
DT[is.na(get(Col_Name))]
See a blog post I wrote on the subject of SE vs NSE in data.table
请参阅我在
data.table
中写的关于 SE 与 NSE 的 博文
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.