unnest_longer gives dollar sign instead of normal tibble

Viewed 185

can anybody explain the following difference:

library(tidyverse)

tribble(~id,
    c(1:10))%>%
unnest_longer(id)%>%
mutate(data = map(.x = id, ~mtcars))%>%
unnest_longer(data)

gives:

# A tibble: 320 x 2
   id data$mpg  $cyl $disp   $hp $drat   $wt $qsec   $vs   $am $gear $carb
   <int>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     21       6  160    110  3.9   2.62  16.5     0     1     4     4
2     1     21       6  160    110  3.9   2.88  17.0     0     1     4     4
3     1     22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
4     1     21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
5     1     18.7     8  360    175  3.15  3.44  17.0     0     0     3     2

whereas

library(tidyverse)

tribble(~id,
    c(1:10))%>%
unnest_longer(id)%>%
mutate(data = map(.x = id, ~mtcars))%>%
unnest(data)

gives the result I want.

      # A tibble: 320 x 12
       id   mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
     <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2     1  21       6  160    110  3.9   2.88  17.0     0     1     4     4
 3     1  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
 4     1  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
 5     1  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2

Why are there $-Signs in the 1st example of code? Thanks in advance!

1 Answers

This is a feature introduced in {tibble} version 2.0.1 - docs.

To get the same results as tidyr::unnest(), you'll need to add tidyr::unpack().

tribble(
  ~id,
  c(1:10)
) %>%
  tidyr::unnest_longer(id) %>%
  dplyr::mutate(data = purrr::map(
    .x = id,
    ~ mtcars
  )) %>%
  tidyr::unnest_longer(data) %>% 
  tidyr::unpack(cols = data)
Related