Kumpelka Kumpelka - 1 month ago 17
R Question

R - Subset rows of a data frame on a condition in all the columns

I want to subset rows of a data frame on a single condition in all the columns, avoiding the use of

subset
.

I understand how to subset a single column but I cannot generalize for all columns (without call all the columns).

Initial data frame :

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


In this example, I want to subset the rows without zeros.

Expected output :

V1 V2 V3
1 1 8 15
2 3 10 17
3 4 11 18
4 7 14 21


Thanks

ira ira
Answer
# 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,]