Home > OS >  Exclude specific rows using filter()
Exclude specific rows using filter()

Time:08-10

I have the following data example

test <- tibble(V1 = c("a", "b", "d", "f"), 
               V2 = c("a", "s", "d", "g"), 
               V3 = c("s", "e", "r", "u"))

I try using filter_all() to exclude rows in which the variables that matches with some value, like that are present:

test %>% filter_all(all_vars(. != c("s", "u")))

But it not works. it give me the following output

# A tibble: 2 × 3
  V1    V2    V3   
  <fct> <fct> <fct>
1 b     s     e    
2 d     d     r 

It's not exclude the row where the values are ("b", "s" and "e").

Thanks all

CodePudding user response:

This might be getting you what you're looking for:

library(dplyr)

test <- tibble(
  V1 = c("a", "b", "d", "f"),
  V2 = c("a", "s", "d", "g"),
  V3 = c("s", "e", "r", "u")
)

test |> filter(if_all(.fns = ~ !(.x %in% c("s", "u"))))
#> # A tibble: 1 × 3
#>   V1    V2    V3   
#>   <chr> <chr> <chr>
#> 1 d     d     r

(Using filter(if_all(...)) as the slightly updated dplyr method)

%in% is checking for the presence of 's' and 'u' across all columns for each row of the data frame.

CodePudding user response:

filter_all() has been superseded, so you probably want to use functions that are replacing it in case newer functions build off of them. Per ?filter_all:

Scoped verbs (_if, _at, _all) have been superseded by the use of across() in an existing verb. See vignette("colwise") for details.

Using across() with filter() is awkward, but there are functions if_all() and if_any() that work in its place nicely for this specific purpose.

library(tibble)
library(dplyr)

test <- tibble(
  V1 = c("a", "b", "d", "f"),
  V2 = c("a", "s", "d", "g"),
  V3 = c("s", "e", "r", "u")
)

# Filter out all rows where s or u appears anywhere
test %>%
  filter(if_all(everything(),
                function(x) {
                  !(x %in% c("s", "u"))
                }))
#> # A tibble: 1 x 3
#>   V1    V2    V3   
#>   <chr> <chr> <chr>
#> 1 d     d     r

CodePudding user response:

  • We can use rowwise from dplyr library
library(dplyr)

test |> 
    rowwise() |>
    filter(!any(c_across() %in% c("s", "u")))
  • output
# A tibble: 1 × 3
# Rowwise: 
  V1    V2    V3   
  <chr> <chr> <chr>
1 d     d     r    
  • Related