我希望在所有列的单个条件中对数据帧的行进行子集化,从而避免使用subset
。
我理解如何对单个列进行子集,但我无法对所有列进行推广(不调用所有列)。
初始数据框:
V1 V2 V3
1 1 8 15
2 2 0 16
3 3 10 17
4 4 11 18
5 5 0 19
6 0 13 20
7 7 14 21
在这个例子中,我想要在没有零的情况下对行进行子集化。
预期产出:
V1 V2 V3
1 1 8 15
2 3 10 17
3 4 11 18
4 7 14 21
由于
答案 0 :(得分:1)
# create your data
a <- c(1,2,3,4,5,0,7)
b <- c(8,0,10,11,0,14,14)
c <- c(15,16,17,18,19,20,21)
data <- cbind(a, b, c)
# filter out rows where there is at least one 0
data[apply(data, 1, min) > 0,]
答案 1 :(得分:0)
匹配到rowSums
后使用0
函数的解决方案。
# creating your data
data <- data.frame(a = c(1,2,3,4,5,0,7),
b = c(8,0,10,11,0,14,14),
c = c(15,16,17,18,19,20,21))
# Selecting rows containing no 0.
data[which(rowSums(as.matrix(data)==0) == 0),]
另一种方式
data[-unique(row(data)[grep("^0$", unlist(data))]),]