Mskhvilidze
Mskhvilidze

Reputation: 15

How to select the rows in R data.frame?

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

Upvotes: 1

Views: 71

Answers (1)

akrun
akrun

Reputation: 886968

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

Upvotes: 0

Related Questions