多个条件中至少两个的子集 [英] subset by at least two out of multiple conditions
问题描述
我发现了很多关于通过多个条件进行子集化的问题,但就是找不到如何通过 至少两个>2 个条件进行子集化.
I found many questions dealing with subsetting by multiple conditions, but just couldn't find how to subset by at least two out of >2 conditions.
这个 SO 问题处理相同的问题,但对所有列应用相同的条件:从所有条件中选择至少具有两个条件的行一个>
This SO question deals with the same problem, but applies the same condition to all columns: Select rows with at least two conditions from all conditions
我的问题是:如何根据三个不同条件中的至少两个条件对行进行子集?
My question is: How can I subset rows by at least two out of three different conditions?
id<-c(1,2,3,4,5)
V1<-c(2,4,4,9,7)
V2<-c(10,20,20,30,20)
V3<-c(0.7,0.1,0.5,0.2,0.9)
df<-data.frame(cbind(id,V1,V2,V3))
我可以通过像这样循环来对满足所有三个条件中的三个条件的行进行子集:
I can subset rows that meet all three out of three conditions by looping through like this:
#empty "subset" data.frame
subdf <- cbind(as.character(),as.numeric(),as.numeric(),as.numeric())
colnames(subdf) <- colnames(df)
for (i in 1:nrow(df)){
if (df$V1[i] <= 4 && df$V2[i] >= 20 && df$V3[i] <= 0.3)
subdf <- rbind(subdf,df[i,])
}
关于如何对满足所有三个条件或两个条件的任意组合的所有行进行子集化的任何想法?
Any ideas on how to subset all rows that fulfill either all three, or any combination of two conditions?
非常感谢!
推荐答案
这里是 LukeA 的答案 的扩展.
dfNew <- df[rowSums(cbind(df$V1 <= 4, df$V2 >= 20, df$V3 <= 0.3)) > 1,]
哪个返回
dfNew
id V1 V2 V3
2 2 4 20 0.1
3 3 4 20 0.5
4 4 9 30 0.2
想法是用 cbind
构造一个逻辑向量矩阵,然后用 rowSums
来计算每行的 TRUE 值的数量.然后可以根据此标准对 data.frame 的行进行子集.
The idea is to construct a matrix of the logical vectors with cbind
and then use rowSums
to count the number of TRUE values for each row. The rows of the data.frame can then be subset based on this criterion.
这篇关于多个条件中至少两个的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!