Search code examples
rdplyrfilteringany

Using any with dplyr


How can I use any in a dplyr pipeline ?

I want to extract rows with at least one TRUE.

mydata = data.frame(V1=c(FALSE,NA,FALSE,TRUE),
                    V2 = c(NA,TRUE,FALSE,TRUE),
                    V3 = c(FALSE,FALSE,FALSE,TRUE))

mydata

     V1    V2    V3
1 FALSE    NA FALSE
2    NA  TRUE FALSE
3 FALSE FALSE FALSE
4  TRUE  TRUE  TRUE


Solution

  • I think you need to use rowwise and c_across if using any, but as pointed out by @akrun, the function if_any nicely combines these elements.

    library(dplyr, warn.conflicts = FALSE)
    
    mydata <- data.frame(
        V1 = c(FALSE, NA, FALSE, TRUE),
        V2 = c(NA, TRUE, FALSE, TRUE),
        V3 = c(FALSE, FALSE, FALSE, TRUE))
    
    mydata %>%
        rowwise() %>%
        filter(any(c_across(starts_with("V")))) %>%
        ungroup()
    #> # A tibble: 2 x 3
    #>   V1    V2    V3   
    #>   <lgl> <lgl> <lgl>
    #> 1 NA    TRUE  FALSE
    #> 2 TRUE  TRUE  TRUE
    
    mydata %>% 
        filter(if_any(starts_with("V")))
    #>     V1   V2    V3
    #> 1   NA TRUE FALSE
    #> 2 TRUE TRUE  TRUE
    

    Created on 2021-06-25 by the reprex package (v2.0.0)