I have the following data frame:
dat <- tibble(
nm = letters[c(1:5)] ,
g1 = c(1, 1, 2, NA, 2),
g2 = c(1, NA, 1, NA, 2),
ga = c(1 :3, NA, 5)
)
It looks like this:
> dat
# A tibble: 5 × 4
nm g1 g2 ga
<chr> <dbl> <dbl> <dbl>
1 a 1 1 1
2 b 1 NA 2
3 c 2 1 3
4 d NA NA NA
5 e 2 2 5
What I want to do is to exclude row when all the column that starts with "g" are NAs. Resulting in this:
nm g1 g2 ga
a 1 1 1
b 1 NA 2
c 2 1 3
e 2 2 5
I tried this but failed:
dat %>%
filter_at(vars(contains("g")), !all_vars(is.na(.)))
That gives:
Error in inherits_any()
:
! Quosures can only be unquoted within a quasiquotation context.
# Bad: list(!!myquosure)
# Good: dplyr::mutate(data, !!myquosure)
CodePudding user response:
What you need is if_all
(or if_any
), which are supplementary functions to across()
.
library(dplyr)
dat %>%
filter( !if_all(starts_with('g'), is.na) )
# # A tibble: 4 × 4
# nm g1 g2 ga
# <chr> <dbl> <dbl> <dbl>
# 1 a 1 1 1
# 2 b 1 NA 2
# 3 c 2 1 3
# 4 e 2 2 5
Equivalently, you could do
dat %>%
filter( if_any(starts_with('g'), ~ !is.na(.x)) )
dat %>%
filter( if_any(starts_with('g'), Negate(is.na)) )
CodePudding user response:
You may try
dat %>%
filter(rowSums(across(starts_with("g"), ~!is.na(.x))) != 0)
nm g1 g2 ga
<chr> <dbl> <dbl> <dbl>
1 a 1 1 1
2 b 1 NA 2
3 c 2 1 3
4 e 2 2 5