如何将过滤器表达式存储为字符串? [英] How to store filter expressions as strings?

查看:62
本文介绍了如何将过滤器表达式存储为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于物种数据库的分析,我经常需要根据项目范围等来更改很多标准.

For the analysis of a species database, I often need to change lots of criteria, depending on the projects scope etc.

由于总是在主脚本本身中更改条件是非常不便的,因此我开始在 exterior parameters.R 文件中将各种参数定义为变量,该文件将被复制到项目特定的文件夹中并进行调整在那里,并且将从 main.R 文件中获取.

As it is very inconvenient to always change the criteria within the main script itself, I started defining various parameters as variables in an exterior parameters.R file which will be copied to the project specific folders and adjusted there, and which will be sourced from the main.R file.

这很好用,但是现在我要过滤表达式,我找不到在参数文件中将它们存储为字符串的方法.

This work great, but now that I come to filter expressions, I can't find a way to store them as a string in my parameters file.

标准过滤器表达式将是以下表达式:

The standard filter expression will be this one:

 rlb == "1" | rlb == "2" | rlb== "3" | rlb == "G" | rlb == "R" | rld ==
 "1" | rld == "2" | rld== "3" | rld == "G" | rld == "R" | ffh2 > 1 | ffh4
 == 1 | ffh5 == 1 | spa1 == 1 | sap == 1

由于某些参数中的"",我无法将其分配为字符串变量,原因是R抱怨存在未知的标记或对象.

Due to the "" in some of the parameters, I can't assign it as a string variable, cause R is complaining that there are unknown tokens or objects.

如何将此过滤器表达式分配给变量,以便以后使用(例如用eval(my_filter_variable)等执行我的过滤?

How can I assign this filter expression to a variable, so I can use it later e.g. with eval(my_filter_variable) etc to perform my filtering?

推荐答案

filter_

您可以在dplyr中使用filter_传递过滤器表达式:

filter_

You can pass your filter expression using filter_ in dplyr:

mtcars %>%
    filter_("cyl == 4")

处理字符串

假设您想进一步处理字符串,可以在过滤器中使用 '' 作为字符串:

Handling strings

Let's say that you want to take this further and handle strings, you could use '' for your string in the filter:

data.frame(col_A = LETTERS[1:10],
           col_B = 1:10,
           stringsAsFactors = FALSE) %>%
    filter_("col_A == 'A'")

处理"

如果您真的想将字符串作为"传递,则必须转义引号:

Handling "

If you really want to pass your string as ", you have to escape quotes:

data.frame(col_A = LETTERS[1:10],
           col_B = 1:10,
           stringsAsFactors = FALSE) %>%
    filter_("col_A == \"A\"")

更好的方法

我建议您避免使用上述方法.看看下面的建议,让您使用sym函数传递列名.在dplyr管道中,您可以使用rlang,这将使您在构建过滤器表达式时更具灵活性:

Better approach

I would suggest that you avoid the approach above. Have a look at the suggestion below that let's you pass your column name using sym function. In dplyr pipeline you could make use of rlang that would give you more flexibility in building your filter expressions:

require(dplyr)
require(rlang)
col_nme <- sym("cyl")
flt_val <- 4
mtcars %>%
    filter(UQ(col_nme) == UQ(flt_val))

这等效于:

mtcars %>%
    filter(UQ(col_nme) == flt_val)

因为您不必取消引用第二个参数.

As you don't have to unquote second argument.

过滤器的语法为:

rlb == "1" | rlb == "2" | rlb== "3" | rlb == "G" | rlb == "R" |

这等同于:

rlb %in% c("1", "2", "3" , "G" , "R")

向量c("1", "2", "3", "G", "R")可以轻松地作为变量传递,而无需进行涉及定量或非标准评估的任何附加工作.我将从简化过滤器开始,然后通过rlang功能使用简化的表达式.

the vector c("1", "2", "3", "G", "R") could be easily passed as a variable, without any addittional effort involving quosures or non-standard evaluation. I would start from simplifying filters then use simplified expressions via rlang features.

在有关代码共享的注释之后,最好查看sqldf软件包:

Following the comment on code sharing, it may be good to look at the sqldf package:

require(sqldf)
sqldf(x = "SELECT * FROM mtcars WHERE CYL = 4")

这可以让您在SQL中共享过滤器,而SQL通常比dplyr语法更熟悉.

This is would let you share your filters in SQL, which is usually more familiar then dplyr syntax.

这篇关于如何将过滤器表达式存储为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆