简体   繁体   English

如何根据列名存储在变量中的列值过滤 data.table

[英]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 .现在我想过滤Col1NA的这个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM