using purrr to map dplyr::select

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]>

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

Tags:

R

Dplyr

Purrr