Home > Software engineering >  How to select the rows in R data.frame?
How to select the rows in R data.frame?

Time:04-21

How can I select the rows, which at least once have value 1 in all 4 columns? or have only 0 through all columns?

CodePudding user response:

We can use filter with if_any/if_all

library(dplyr)
df1 %>%
    filter(if_any(everything(), ~ .== 1)|if_all(everything(), ~ . == 0))

Or with base R

df1[(rowSums(df1 == 1) > 0) | (rowSums(df1 == 0) == ncol(df1)),]
  • Related