Skip to content

drop_na() incompatible with list-cols #280

Closed

Description

@tiernanmartin

It seems that drop_na()'s dependency on stats::complete.cases is tripping it up when any list-cols are present:

library(tidyverse) 
library(purrr)

set.seed(1)

# Give mtcars a list-col and some NAs
mtcars_nas <- 
  mtcars %>% 
  rownames_to_column() %>% 
  mutate(LIST_COL = map(mpg, ~sample(LETTERS, nchar(.x)))) %>% 
  mutate(mpg = sample(c(mpg, rep(NA, 32)), n(), replace = TRUE)) %>% 
  select(rowname, mpg, LIST_COL) %>% 
  as_tibble

mtcars_nas
#> # A tibble: 32 × 3
#>              rowname   mpg  LIST_COL
#>                <chr> <dbl>    <list>
#> 1          Mazda RX4    NA <chr [2]>
#> 2      Mazda RX4 Wag  21.4 <chr [2]>
#> 3         Datsun 710  15.0 <chr [4]>
#> 4     Hornet 4 Drive  16.4 <chr [4]>
#> 5  Hornet Sportabout    NA <chr [4]>
#> 6            Valiant  19.7 <chr [4]>
#> 7         Duster 360    NA <chr [4]>
#> 8          Merc 240D  15.2 <chr [4]>
#> 9           Merc 230  10.4 <chr [4]>
#> 10          Merc 280    NA <chr [4]>
#> # ... with 22 more rows

# Try to drop the incomplete rows - fails
mtcars_nas %>% tidyr::drop_na()
#> Error in stats::complete.cases(data): invalid 'type' (list) of argument

Can you add a check to drop_na() to override this behavior?

Activity

  1. jameshowison commented on Jul 20, 2017

    @jameshowison

    Unsurprisingly I also have this problem when I have NAs in a list column, so I'm commenting to add that possibility to the example above (where the NAs are in other, non-list columns).

  2. hadley commented on Nov 16, 2017

    @hadley
    Member

    NB: I'm pretty sure I worked around this problem in ggplot2, so the code should already exist.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementrectangling 🗄️converting deeply nested lists into tidy data frames

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions