I have a list of roughly 50 somewhat complicated grepl commands strings that I would like to pass to a function in R. I am using these functions to subset a dataframe. Here is an example of 2 of the commands:
我有一个大约50个有点复杂的grepl命令字符串的列表,我想传递给R中的函数。我使用这些函数来子集数据帧。以下是2个命令的示例:
t <- subDF[grepl("(extreme|abnormal|unseasonably|unusually|record|excessive) (heat|warm|high temp)",subDF$EVTYPE),]
t <- subDF[grepl("fl(oo)?d",subDF$EVTYPE) & !grepl("flash",subDF$EVTYPE) & !grepl("(tidal|beach|(c(oa)?sta?l))(/tidal)? ?(flood)",subDF$EVTYPE),]
So, in this example I would like to pass pass these 2 grepl commands to a function that will do this subsetting on dataframe subDF (plus pass the other 48 or so).
因此,在这个例子中,我希望将这两个grepl命令传递给一个函数,该函数将在dataframe subDF上执行此子集化(另外传递其他48个左右)。
Any elegant way to do this?
有什么优雅的方法吗?
2 个解决方案
#1
1
Here's an example that uses quote
to create two unevaluated grepl
calls. They are then evaluated in an sapply
call with eval
.
这是一个使用quote创建两个未评估的grepl调用的示例。然后使用eval在一个sapply调用中评估它们。
> fruits <- c("one apple", "two pears", "three bananas")
> QQ <- list(q1 = quote(grepl("(one)|(apple)", fruits)),
q2 = quote(grepl("apple", fruits) | grepl("bananas|one", fruits)))
> sapply(QQ, function(x) fruits[eval(x)])
#$q1
#[1] "one apple"
#
#$q2
#[1] "one apple" "three bananas"
A look at QQ
一看QQ
#$q1
#grepl("(one)|(apple)", fruits)
#
#$q2
#grepl("apple", fruits) | grepl("bananas|one", fruits)
Something else that is useful is
其他有用的东西是
> as.list(quote(grepl("one|apple", fruits)))
# [[1]]
# grepl
#
# [[2]]
# [1] "one|apple"
#
# [[3]]
# fruits
With this, you can replace the regular expression (or call or x
) in every iteration by way of [[
indexing.
有了这个,你可以通过[[indexing]的方式在每次迭代中替换正则表达式(或call或x)。
#2
0
It looks like you're after Reduce
. Using the iris dataset as an example:
看起来你在Reduce之后。以虹膜数据集为例:
mygrep <- function(x, df) df[grepl(x, df$Species), ]
pat <- c("setosa", "(setosa|virginica)")
Reduce(mygrep, pat, iris, right=TRUE)
#1
1
Here's an example that uses quote
to create two unevaluated grepl
calls. They are then evaluated in an sapply
call with eval
.
这是一个使用quote创建两个未评估的grepl调用的示例。然后使用eval在一个sapply调用中评估它们。
> fruits <- c("one apple", "two pears", "three bananas")
> QQ <- list(q1 = quote(grepl("(one)|(apple)", fruits)),
q2 = quote(grepl("apple", fruits) | grepl("bananas|one", fruits)))
> sapply(QQ, function(x) fruits[eval(x)])
#$q1
#[1] "one apple"
#
#$q2
#[1] "one apple" "three bananas"
A look at QQ
一看QQ
#$q1
#grepl("(one)|(apple)", fruits)
#
#$q2
#grepl("apple", fruits) | grepl("bananas|one", fruits)
Something else that is useful is
其他有用的东西是
> as.list(quote(grepl("one|apple", fruits)))
# [[1]]
# grepl
#
# [[2]]
# [1] "one|apple"
#
# [[3]]
# fruits
With this, you can replace the regular expression (or call or x
) in every iteration by way of [[
indexing.
有了这个,你可以通过[[indexing]的方式在每次迭代中替换正则表达式(或call或x)。
#2
0
It looks like you're after Reduce
. Using the iris dataset as an example:
看起来你在Reduce之后。以虹膜数据集为例:
mygrep <- function(x, df) df[grepl(x, df$Species), ]
pat <- c("setosa", "(setosa|virginica)")
Reduce(mygrep, pat, iris, right=TRUE)