0

Example data frame:

test <- data.frame(A=c(0,1,2), B=c(0,2,3), C=c(0,1,0))
test
  A B C
1 0 0 0
2 1 2 1
3 2 3 0

What I am trying to get:

  A B C
2 1 2 1

When a row contains ≥1 cell equals to 0, discard it.

I am aware of the following, but I am looking for a more general solution, in the case I don't know how many columns my data frame is made of.

test[which(test$A !=0 & test$B !=0 & test$C != 0),]
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
user31888
  • 421
  • 6
  • 13

1 Answers1

2

We can use rowSums to subset the rows where there are no zeroes.

test[rowSums(test == 0) == 0, ]

#  A B C
#2 1 2 1
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Woaw ! So reactive, I still have to wait 10 minutes before being able to validate the answer :-) Thanks ! – user31888 Nov 30 '18 at 09:37