简体   繁体   English

有没有一种快速的方法可以多次过滤data.frame的行?

[英]Is there a quick way of filtering rows of a data.frame more than once?

I have a dataframe xd from which I wish to filter data for id=1,2, but with 1 and 2 both repeated twice. 我有一个数据框xd ,我希望从其中过滤ID = 1,2的数据,但1和2都重复两次。

set.seed(12)
xd <- data.frame(id = sort(sample(3,20, rep=TRUE)), y = rnorm(20))
fxd <- subset(xd, subset = id %in% c(1,2,1,2)) # doesn't work
str(fxd)

However, this doesn't work because it only selects id=1 and id=2 only once. 但是,这不起作用,因为它仅选择一次id = 1和id = 2。 Is there any quick way of getting around it? 有什么快速的解决方法吗?

The subset argument of function subset expects logical expression, meaning you can select from rows by mentioning TRUE/FALSE for each row. 函数subsetsubset参数需要逻辑表达式,这意味着您可以通过为每行提及TRUE/FALSE从行中进行选择。

If you want to replicate selection then an option is to use which . 如果要复制选择,则可以选择使用which Which returns row-number that can be replicated. 它返回可以复制的row-number Hence, option can be as: 因此,选项可以是:

set.seed(12)
xd <- data.frame(id = sort(sample(3,20, rep=TRUE)), y = rnorm(20))

fxd <- xd[rep(which(xd$id %in% c(1,2)), each = 2),]

fxd
#      id           y
# 1     1 -0.77771958
# 1.1   1 -0.77771958
# 2     1 -1.29388230
# 2.1   1 -1.29388230
# 3     1 -0.77956651
# 3.1   1 -0.77956651
# 4     1  0.01195176
# 4.1   1  0.01195176
# 5     1 -0.15241624
# 5.1   1 -0.15241624
# 6     1 -0.70346425
# 6.1   1 -0.70346425
# 7     1  1.18887916
# 7.1   1  1.18887916
# 8     1  0.34051227
# 8.1   1  0.34051227

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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