[英]Using a vector of logical expressions to subset dataframe in R
I am trying to use a vector of logical expressions to subset a data frame. 我试图使用逻辑表达式向量来子集数据帧。 As a simple example, here is a data frame that I will subset using a logical expression.
举个简单的例子,这里是一个数据框,我将使用逻辑表达式进行子集化。 First, I'll type in the logical expression manually:
首先,我将手动输入逻辑表达式:
> dat <- data.frame(x = c(0,0,1,1), y = c(0,0,0,1), z = c(0,1,1,1))
> dat
x y z
1 0 0 0
2 0 0 1
3 1 0 1
4 1 1 1
> subset(dat, x == 1)
x y z
3 1 0 1
4 1 1 1
If I have a vector of logical expressions, how can I call from that vector and apply them to a subsetting method? 如果我有一个逻辑表达式向量,我如何从该向量调用并将它们应用于子集方法? Here is one way that doesn't work:
这是一种不起作用的方式:
> criteria <- as.factor(c("x == 1", "y == 1", "y == 1 & z == 1"))
> subset(dat, criteria[1])
Error in subset.data.frame(dat, criteria[1]) : 'subset' must be logical
Any suggestions? 有什么建议么?
You can't make an atomic vector of vectors, so you can contain them in a list. 您不能制作矢量的原子矢量,因此您可以将它们包含在列表中。 Subsetting with
[
, which is easier to program with than subset
, 使用
[
进行subset
,比subset
更容易编程,
dat <- data.frame(x = c(0,0,1,1),
y = c(0,0,0,1),
z = c(0,1,1,1))
indices <- list(dat$x == 1,
dat$y == 1,
dat$x == 1 & dat$z == 1)
str(indices)
#> List of 3
#> $ : logi [1:4] FALSE FALSE TRUE TRUE
#> $ : logi [1:4] FALSE FALSE FALSE TRUE
#> $ : logi [1:4] FALSE FALSE TRUE TRUE
dat[indices[[1]], ]
#> x y z
#> 3 1 0 1
#> 4 1 1 1
lapply(indices, function(i) dat[i, ])
#> [[1]]
#> x y z
#> 3 1 0 1
#> 4 1 1 1
#>
#> [[2]]
#> x y z
#> 4 1 1 1
#>
#> [[3]]
#> x y z
#> 3 1 0 1
#> 4 1 1 1
We can use parse
and eval
to evaluate the condition as a vector of string. 我们可以使用
parse
和eval
来评估条件作为字符串的向量。
criteria <- c("x == 1", "y == 1", "y == 1 & z == 1")
subset(dat, eval(parse(text = criteria)))
# x y z
# 4 1 1 1
We can use index to select the element in the criteria
vector to subset the data frame. 我们可以使用index来选择
criteria
向量中的元素来对数据帧进行子集化。
subset(dat, eval(parse(text = criteria[1])))
# x y z
# 3 1 0 1
# 4 1 1 1
With a vector of logicals: 使用逻辑向量:
critearia <- dat$x == 1 & dat$y == 1 & dat$z == 1
subset(dat, critearia)
Directly: 直:
subset(dat, x == 1 & y == 1 & z == 1)
With data.table
: 使用
data.table
:
library(data.table)
dat <- as.data.table(dat)
dat[x == 1 & y == 1 & z == 1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.