8
votes

I have a data frame with a bunch of nested data-frames within it, and I'd like to apply dplyr::select to each of those nested data frames. Here's an example

 library(tidyverse)

 mtcars %>%
 group_by(cyl) %>%
 nest %>%
 mutate(data2 = ~map(data, dplyr::select(.,-mpg)))

I would think that this would result in a data frame with three columns. cyl: the number of cylinders, data: the nested data, data2: the same as data except each element would not have the mpg column.

Instead R crashes:

 *** caught segfault ***
address 0x7ffc1e445000, cause 'memory not mapped'

Traceback:
 1: .Call(`_dplyr_mutate_impl`, df, dots)
 2: mutate_impl(.data, dots)
 3: mutate.tbl_df(., data2 = ~map(data, dplyr::select(., -mpg)))
 4: mutate(., data2 = ~map(data, dplyr::select(., -mpg)))
 5: function_list[[k]](value)
 6: withVisible(function_list[[k]](value))
 7: freduce(value, `_function_list`)
 8: `_fseq`(`_lhs`)
 9: eval(quote(`_fseq`(`_lhs`)), env, env)
10: eval(quote(`_fseq`(`_lhs`)), env, env)
11: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
12: mtcars %>% group_by(cyl) %>% nest %>% mutate(data2 = ~map(data,     dplyr::select(., -mpg)))

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace

I realize I could get the columns I wanted if I apply the select operation before the nesting, but this would be less analogous with my real problem. Could somebody please explain to me what I am doing wrong here? Thanks for any advice.

3
Could you retry the code but with mutate(data = map(data, function(x) dplyr::select(x, -mpg)))Russ Hyde
Crashes are always reportable. You should describe the versions of R and all the packages with sessionInfo()-output.IRTFM
Got it, I'll submit a bug-report as well.ohnoplus

3 Answers

6
votes

You need to move ~ from map to select; or use the comment as @Russ; ~ is used when the function (in this case purrr::map) accepts a formula as argument:

mtcars %>%
    group_by(cyl) %>%
    nest %>%
    mutate(data2 = map(data, ~ select(., -mpg)))

# A tibble: 3 x 3
#    cyl data               data2            
#  <dbl> <list>             <list>           
#1     6 <tibble [7 × 10]>  <tibble [7 × 9]> 
#2     4 <tibble [11 × 10]> <tibble [11 × 9]>
#3     8 <tibble [14 × 10]> <tibble [14 × 9]>
3
votes

Here's 2 ways: one skips nesting and just uses do, and one nests and then uses a map. unnest(data2) then gets it back into a regular data frame. One thing to note is that I included -cyl inside the select in the first example; that's because otherwise, you end up with cyl twice, once from the grouping column and once from the unnested data frame.

I'm not sure if there's a way one of these is better than the other, besides personal preference.

library(tidyverse)

mtcars %>%
    group_by(cyl) %>%
    do(data2 = select(., -mpg, -cyl)) %>%
    unnest(data2)
#> # A tibble: 32 x 10
#>      cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1     4 108      93  3.85  2.32  18.6     1     1     4     1
#>  2     4 147.     62  3.69  3.19  20       1     0     4     2
#>  3     4 141.     95  3.92  3.15  22.9     1     0     4     2
#>  4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
#>  5     4  75.7    52  4.93  1.62  18.5     1     1     4     2
#>  6     4  71.1    65  4.22  1.84  19.9     1     1     4     1
#>  7     4 120.     97  3.7   2.46  20.0     1     0     3     1
#>  8     4  79      66  4.08  1.94  18.9     1     1     4     1
#>  9     4 120.     91  4.43  2.14  16.7     0     1     5     2
#> 10     4  95.1   113  3.77  1.51  16.9     1     1     5     2
#> # ... with 22 more rows

mtcars %>%
    group_by(cyl) %>%
    nest() %>%
    mutate(data2 = map(data, function(df) select(df, -mpg))) %>%
    unnest(data2)
# same output
1
votes

An alternative solution is to just pass -mpg "as is" to map(), which will correctly pass it down to select().

mtcars %>%
  group_by(cyl) %>%
  nest %>%
  mutate(data2 = map( data, select, -mpg ))

Works in R 3.6.1 with dplyr 0.8.3.