R - 所有列中条件的数据框的子集行

时间:2016-11-08 16:07:51

标签: r dataframe subset

我希望在所有列的单个条件中对数据帧的行进行子集化,从而避免使用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

由于

2 个答案:

答案 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))]),]