Skip to content

Instantly share code, notes, and snippets.

@jennybc
Last active June 26, 2019 15:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennybc/a5c0710ec9d024cbd5c289a3d7569267 to your computer and use it in GitHub Desktop.
Save jennybc/a5c0710ec9d024cbd5c289a3d7569267 to your computer and use it in GitHub Desktop.
Faking tidyr::drop_na(..., logic = "all")
library(tidyverse)
(df <- tibble(
x = c(1, NA, 3, NA),
y = c(1, NA, NA, 4),
z = 1:4
))
#> # A tibble: 4 × 3
#> x y z
#> <dbl> <dbl> <int>
#> 1 1 1 1
#> 2 NA NA 2
#> 3 3 NA 3
#> 4 NA 4 4
df %>% drop_na(x:y)
#> # A tibble: 1 × 3
#> x y z
#> <dbl> <dbl> <int>
#> 1 1 1 1
allNA <- . %>% map(is.na) %>% flatten_lgl() %>% all()
df %>%
filter(!pmap_lgl(select(., x:y), lift_ld(allNA)))
#> # A tibble: 3 × 3
#> x y z
#> <dbl> <dbl> <int>
#> 1 1 1 1
#> 2 3 NA 3
#> 3 NA 4 4
@jennybc
Copy link
Author

jennybc commented Apr 27, 2017

Provoked by https://twitter.com/noamross/status/857609829480792064

Is there a simple idiom for removing data frame rows that are all NA, (or in which all of a range of columns are NA)?

@bhive01
Copy link

bhive01 commented Apr 27, 2017

Well, if you were provoked...

This is good because you can select the columns you want to be sensitive to the operation. The other options are less specific or specific to all columns being NA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment