Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206316

tibble::add_row to nested tibble throws error under tidyr 1.0.0

$
0
0

In a former version of tidyr I was able to add rows to a nested tibble using tibble::add_row. After updating to version 1.0.0 I get the following error:

Error: levels.vctrs_list_of() not supported.

library(dplyr, warn.conflicts = FALSE)
library(tibble)
library(tidyr) # version 1.0.0

mtcars %>% 
  tidyr::nest(data = dplyr::select(., -cyl) %>% colnames) %>% 
  tibble::add_row(cyl = "all cyl", data = NA)
#> Error: `levels.vctrs_list_of()` not supported.

Created on 2020-01-17 by the reprex package (v0.3.0)

And here is the same call using tidyr 0.8.3

library(dplyr, warn.conflicts = FALSE)
library(tibble)
library(tidyr) # version 0.8.3

 mtcars %>% 
   tidyr::nest(-cyl) %>% 
   tibble::add_row(cyl = "all cyl", data = NA)
  #> # A tibble: 4 x 2
  #>   cyl     data              
  #>   <chr>   <list>            
  #> 1 6       <tibble [7 × 10]> 
  #> 2 4       <tibble [11 × 10]>
  #> 3 8       <tibble [14 × 10]>
  #> 4 all cyl <lgl [1]>

Created on 2020-01-17 by the reprex package (v0.3.0)

Are other users experiencing the same or is this specific to my system environment? Do I need to update other packages to get this running? Is there a workaround? Should I open an issue on Github?

Update: In the comments someone suggested updating the vctrs package. But after updating to the latest version 0.2.1 the same error still appears.

background / context

I should elaborate why I wanted to use add_row with data = NA in the first place.

My original script looked somewhat like this:

# tidyr version 0.8.3 add_row was working
iris %>% 
  tidyr::nest(-Species) %>% 
  tibble::add_row(Species = "All species", data = NA) %>% 
  mutate(data = purrr::modify_at(4, ~ as_tibbleselect(iris, -Species)))

Which returned a nested tibble with all categories as well as an overall category.

Under tidyr 1.0.0 my code looks like this and throws said error:

# tidyr version 1.0.0 where add_row does not work
iris %>% 
  tidyr::nest(data = dplyr::select(., -Species) %>% colnames) %>% 
  tibble::add_row(Species = "All species", data = NA) %>% 
  mutate(data = purrr::modify_at(data, nrow(.), ~ as_tibble(select(iris, -Species))))

Since add_row is not working under tidyr 1.0.0 anymore, the next best similar alternative is as @IceCreamToucan suggested bind_rows.

# tidyr version 1.0.0 alternative with dpylr::bind_rows
iris %>% 
  tidyr::nest(data = dplyr::select(., -Species) %>% colnames) %>% 
  dplyr::bind_rows(tibble(Species = "All species", data = NA)) %>% 
  mutate(data = purrr::modify_at(data, nrow(.), ~ as_tibble(select(iris, -Species))))

However, since the syntax of tidyr::nest got more verbose under version 1.0.0 I tried to streamline the code and this seems by far the most straightforward approach, which I should have chosen from beginning on:

# What I should have been doing in the first place
iris %>% 
  dplyr::bind_rows(mutate(iris, Species = "All species")) %>% 
  tidyr::nest(data = dplyr::select(., -Species) %>% colnames) 

Viewing all articles
Browse latest Browse all 206316

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>