使用变量名过滤dplyr的tbl_df

时间:2021-05-02 23:27:52

I am having trouble using dplyr's tbl_df, respectively the regular data.frame. I got a big tbl_df (500x30K) and need to filter it. So what I would like to do is:

我在使用dplyr的tbl_df时遇到了麻烦,它们分别是常规的data.frame。我有一个很大的tbl_df (500x30K)需要过滤它。所以我想做的是:

filter(my.tbl_df, row1>0, row10<0)

which would be similar to

这和什么相似呢?

df[df$row1>0 & df$row10<0,]

Works great. But I need to build the filter functions dynamically while running, so I need to access the DF/tbl_df columns by one or multiple variables. I tried something like:

伟大的工作。但是我需要在运行时动态构建过滤器函数,所以我需要通过一个或多个变量访问DF/tbl_df列。我试着喜欢的东西:

var=c("row1","row10")
op=c(">","<")
val=c(0,0)
filter(my.tbl_df, eval(parse(text=paste(var,op,val,sep="")))

Which gives me an error: not compatible with LGLSXP This seems to be deeply rooted in the Cpp code.

这给了我一个错误:与LGLSXP不兼容,这似乎深深植根于Cpp代码中。

I would be thankful for any hint. Also pointing out the "string to environment variable" conversion would be helpful, since I am pretty that I am doing it wrong.

我会感激任何暗示。同时指出“字符串到环境变量”的转换将会很有帮助,因为我做错了。

With best,

用最好的,

Mario

马里奥

1 个解决方案

#1


4  

This is related to this issue. In the meantime, one way could be to construct the whole expression, i.e.:

这与这个问题有关。同时,一种方法是构造整个表达式,即:

> my.tbl_df <- data.frame( row1 = -5:5, row10 = 5:-5)
> call <- parse( text = sprintf( "filter(my.tbl_df, %s)", paste(var,op,val, collapse="&") ) )
> call 
expression(filter(my.tbl_df, row1 > 0&row10 < 0))
> eval( call )
  row1 row10
1    1    -1
2    2    -2
3    3    -3
4    4    -4
5    5    -5

#1


4  

This is related to this issue. In the meantime, one way could be to construct the whole expression, i.e.:

这与这个问题有关。同时,一种方法是构造整个表达式,即:

> my.tbl_df <- data.frame( row1 = -5:5, row10 = 5:-5)
> call <- parse( text = sprintf( "filter(my.tbl_df, %s)", paste(var,op,val, collapse="&") ) )
> call 
expression(filter(my.tbl_df, row1 > 0&row10 < 0))
> eval( call )
  row1 row10
1    1    -1
2    2    -2
3    3    -3
4    4    -4
5    5    -5