简体   繁体   中英

Filter rows that contain specific boolean value in any column in a dataframe in R

Let's say I have a data frame:

data <- data.frame(w = c(1, 2, 3, 4), x = c(F, F, F, F), y = c(T, T, F, T), 
                   z = c(T, F, F, T), z1 = c(12, 4, 5, 15))

data
#>   w     x     y     z z1
#> 1 1 FALSE  TRUE  TRUE 12
#> 2 2 FALSE  TRUE FALSE  4
#> 3 3 FALSE FALSE FALSE  5
#> 4 4 FALSE  TRUE  TRUE 15

Question

How do I filter the rows in which all boolean variables are FALSE ? In this case, row 3 . Or in other words, I would like to get a data frame that has at least one TRUE value per row.

Expected output

#>   w     x     y     z z1
#> 1 1 FALSE  TRUE  TRUE 12
#> 2 2 FALSE  TRUE FALSE  4
#> 3 4 FALSE  TRUE  TRUE 15

Attempt

library(tidyverse)
data %>% filter(x == T | y == T | z == T)

#>  w     x    y     z z1
#> 1 1 FALSE TRUE  TRUE 12
#> 2 2 FALSE TRUE FALSE  4
#> 3 4 FALSE TRUE  TRUE 15

Above is a working option, but not scalable at all. Is there a more convenient option using the dplyr's filter() function?

rowSums() is a good option - TRUE is 1, FALSE is 0.

cols = c("x", "y", "z")

## all FALSE
df[rowSums[cols] == 0, ]

## at least 1 TRUE
df[rowSums[cols] >= 1, ]

## etc.

With dplyr , I would use the same idea like this:

df %>%
  filter(
    rowSums(. %>% select(all_of(cols))) >= 1
  )
# after @Gregor Thomas's suggestion on using TRUE or FALSE
df[!(apply(!df[, c('x', 'y', 'z')], 1, all)), ]

# without rowSums
df[!(apply(df[, c('x', 'y', 'z')] == FALSE, 1, all)), ]

# with rowSums
df[rowSums(df[, c('x', 'y', 'z')] == FALSE) != 3, ]
#  w     x    y     z z1
#1 1 FALSE TRUE  TRUE 12
#2 2 FALSE TRUE FALSE  4
#4 4 FALSE TRUE  TRUE 15

With dplyr's filter() ,

library(dplyr)

filter(data, (x + y + z) > 0 )

  w     x    y     z z1
1 1 FALSE TRUE  TRUE 12
2 2 FALSE TRUE FALSE  4
3 4 FALSE TRUE  TRUE 15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM