使用purrr映射dplyr :: select

时间:2018-05-11 18:59:40

标签: r dplyr purrr

我有一个数据框,其中包含一堆嵌套数据框,我想将dplyr :: select应用于每个嵌套数据框。这是一个例子

 library(tidyverse)

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

我认为这会导致数据框有三列。 cyl:柱面数data:嵌套数据data2:与数据相同,但每个元素都没有mpg列。

相反,R崩溃了:

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

我意识到如果我在嵌套之前应用选择操作,我可以获得我想要的列,但这与我的实际问题不太相似。有人可以向我解释我在这里做错了什么吗?谢谢你的任何建议。

3 个答案:

答案 0 :(得分:4)

您需要将~map移至select;或者将评论用作@Russ;函数(在本例中为~)接受公式作为参数时使用purrr::map

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

答案 1 :(得分:1)

以下两种方式:一种方法是跳过嵌套,只使用do,一种嵌套然后使用map。然后unnest(data2)将其恢复为常规数据框。需要注意的一点是,我在第一个示例中的-cyl中包含了select;这是因为否则,您最终会使用cyl两次,一次来自分组列,一次来自未建模的数据框。

除了个人喜好之外,我不确定其中一种方式是否优于另一种方式。

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)
#> # 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     6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2     6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  4     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  5     6  168.   123  3.92  3.44  18.3     1     0     4     4
#>  6     6  168.   123  3.92  3.44  18.9     1     0     4     4
#>  7     6  145    175  3.62  2.77  15.5     0     1     5     6
#>  8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  9     4  147.    62  3.69  3.19  20       1     0     4     2
#> 10     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> # ... with 22 more rows

reprex package(v0.2.0)创建于2018-05-11。

答案 2 :(得分:0)

另一种解决方案是仅按原样将-mpg传递给map(),这将正确地传递给select()

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

通过R 3.6.1dplyr 0.8.3中工作。