(R) 将 map() 与列表列一起使用的更简洁方法 [英] (R) Cleaner way to use map() with list-columns
问题描述
我正在尝试从 rowwise() 中移除列表列,因为我听说 tidyverse 团队正在取消它.但是,我不习惯使用 purrr 函数,所以我觉得必须有更好的方法来执行以下操作:
I am trying to move away from rowwise() for list columns as I have heard that the tidyverse team is in the process of axing it. However, I am not used to using the purrr functions so I feel like there must be a better way of doing the following:
我为每个物种创建了一个包含一个小标题的列表列.然后我想进入tibble并取某些变量的平均值.第一种情况是使用地图,第二种情况是我个人觉得更干净的 rowwise 解决方案.
I create a list-column containing a tibble for each species. I then want to go into the tibble and take the mean of certain variables. The first case is using map and second is the rowwise solution that I personally feel is cleaner.
有谁知道在这种情况下使用地图的更好方法吗?
Does anyone know a better way to use map in this situation?
library(tidyverse)
iris %>%
group_by(Species) %>%
nest() %>%
mutate(mean_slength = map_dbl(data, ~mean(.$Sepal.Length, na.rm = TRUE)),
mean_swidth = map_dbl(data, ~mean(.$Sepal.Width, na.rm = TRUE))
)
#> # A tibble: 3 x 4
#> Species data mean_slength mean_swidth
#> <fct> <list> <dbl> <dbl>
#> 1 setosa <tibble [50 x 4]> 5.01 3.43
#> 2 versicolor <tibble [50 x 4]> 5.94 2.77
#> 3 virginica <tibble [50 x 4]> 6.59 2.97
iris %>%
group_by(Species) %>%
nest() %>%
rowwise() %>%
mutate(mean_slength = mean(data$Sepal.Length, na.rm = TRUE),
mean_swidth = mean(data$Sepal.Width, na.rm = TRUE))
#> Source: local data frame [3 x 4]
#> Groups: <by row>
#>
#> # A tibble: 3 x 4
#> Species data mean_slength mean_swidth
#> <fct> <list> <dbl> <dbl>
#> 1 setosa <tibble [50 x 4]> 5.01 3.43
#> 2 versicolor <tibble [50 x 4]> 5.94 2.77
#> 3 virginica <tibble [50 x 4]> 6.59 2.97
由 reprex 包 (v0.2.1) 于 2018 年 12 月 26 日创建上>
Created on 2018-12-26 by the reprex package (v0.2.1)
推荐答案
不要使用两个 map
,而是使用一个 summarise_at
Instead of having two map
, use a single one, with summarise_at
library(tidyverse)
iris %>%
group_by(Species) %>%
nest() %>%
mutate(out = map(data, ~
.x %>%
summarise_at(vars(matches('Sepal')),
funs(mean_s = mean(., na.rm = TRUE))))) %>%
unnest(out)
这篇关于(R) 将 map() 与列表列一起使用的更简洁方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!