任何人都可以提出关于何时使用map()(所有map _ ..()函数)以及何时使用summarise_at()/ mutate_at()?的建议
例如,如果我们对向量列进行一些修改,那么我们不需要考虑map()?如果我们有一个df /有一个列有一个列表,那么我们需要使用map()?
map()功能是否总是需要与nest()功能一起使用?任何人都可以建议一些关于此的学习视频 还有如何将列表放在df中并同时建模多个列表,然后将模型结果存储在另一列中?
非常感谢!
Col*_*FAY 19
{dplyr}和{purrr}之间的最大区别是{dplyr}仅用于处理data.frames,而{purrr}旨在用于各种列表.Data.frames是列表,您也可以使用{purrr}来迭代data.frame.
map_chr(iris, class)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
"numeric" "numeric" "numeric" "numeric" "factor"
Run Code Online (Sandbox Code Playgroud)
summarise_at并且map_at不完全相同:summarise_at只返回您要查找的摘要,map_at将所有data.frame作为列表返回,并在您询问的地方完成修改:
> library(purrr)
> library(dplyr)
> small_iris <- sample_n(iris, 5)
> map_at(small_iris, c("Sepal.Length", "Sepal.Width"), mean)
$Sepal.Length
[1] 6.58
$Sepal.Width
[1] 3.2
$Petal.Length
[1] 6.7 1.3 5.7 4.3 4.7
$Petal.Width
[1] 2.0 0.4 2.1 1.3 1.5
$Species
[1] virginica setosa virginica versicolor versicolor
Levels: setosa versicolor virginica
> summarise_at(small_iris, c("Sepal.Length", "Sepal.Width"), mean)
Sepal.Length Sepal.Width
1 6.58 3.2
Run Code Online (Sandbox Code Playgroud)
map_at总是返回一个列表,mutate_at总是一个data.frame:
> map_at(small_iris, c("Sepal.Length", "Sepal.Width"), ~ .x / 10)
$Sepal.Length
[1] 0.77 0.54 0.67 0.64 0.67
$Sepal.Width
[1] 0.28 0.39 0.33 0.29 0.31
$Petal.Length
[1] 6.7 1.3 5.7 4.3 4.7
$Petal.Width
[1] 2.0 0.4 2.1 1.3 1.5
$Species
[1] virginica setosa virginica versicolor versicolor
Levels: setosa versicolor virginica
> mutate_at(small_iris, c("Sepal.Length", "Sepal.Width"), ~ .x / 10)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 0.77 0.28 6.7 2.0 virginica
2 0.54 0.39 1.3 0.4 setosa
3 0.67 0.33 5.7 2.1 virginica
4 0.64 0.29 4.3 1.3 versicolor
5 0.67 0.31 4.7 1.5 versicolor
Run Code Online (Sandbox Code Playgroud)
因此,总结一下你的第一个问题,如果你正在考虑在非嵌套df上进行"逐列"操作并希望得到一个data.frame,你应该选择{dplyr}.
对于嵌套列,你必须结合起来group_by(),nest()从{} tidyr,mutate()和map().你在这里做的是创建一个较小版本的数据框,其中包含一个dataeframe列表列.然后,您将使用map()迭代这个新列中的元素.
以下是我们心爱的虹膜的一个例子:
library(tidyr)
iris_n <- iris %>%
group_by(Species) %>%
nest()
iris_n
# A tibble: 3 x 2
Species data
<fct> <list>
1 setosa <tibble [50 × 4]>
2 versicolor <tibble [50 × 4]>
3 virginica <tibble [50 × 4]>
Run Code Online (Sandbox Code Playgroud)
这里,新对象是一个data.frame,其中colum data是一个较小的data.frames列表,一个是Species(我们指定的因子group_by()).然后,我们可以通过简单地执行以下操作来迭代此列:
map(iris_n$data, ~ lm(Sepal.Length ~ Sepal.Width, data = .x))
[[1]]
Call:
lm(formula = Sepal.Length ~ Sepal.Width, data = .x)
Coefficients:
(Intercept) Sepal.Width
2.6390 0.6905
[[2]]
Call:
lm(formula = Sepal.Length ~ Sepal.Width, data = .x)
Coefficients:
(Intercept) Sepal.Width
3.5397 0.8651
[[3]]
Call:
lm(formula = Sepal.Length ~ Sepal.Width, data = .x)
Coefficients:
(Intercept) Sepal.Width
3.9068 0.9015
Run Code Online (Sandbox Code Playgroud)
但我们的想法是将所有内容保存在data.frame中,这样我们就可以mutate创建一个列来保存这个新的lm结果列表:
iris_n %>%
mutate(lm = map(data, ~ lm(Sepal.Length ~ Sepal.Width, data = .x)))
# A tibble: 3 x 3
Species data lm
<fct> <list> <list>
1 setosa <tibble [50 × 4]> <S3: lm>
2 versicolor <tibble [50 × 4]> <S3: lm>
3 virginica <tibble [50 × 4]> <S3: lm>
Run Code Online (Sandbox Code Playgroud)
所以你可以运行几个mutate()来得到r.squared例如:
iris_n %>%
mutate(lm = map(data, ~ lm(Sepal.Length ~ Sepal.Width, data = .x)),
lm = map(lm, summary),
r_squared = map_dbl(lm, "r.squared"))
# A tibble: 3 x 4
Species data lm r_squared
<fct> <list> <list> <dbl>
1 setosa <tibble [50 × 4]> <S3: summary.lm> 0.551
2 versicolor <tibble [50 × 4]> <S3: summary.lm> 0.277
3 virginica <tibble [50 × 4]> <S3: summary.lm> 0.209
Run Code Online (Sandbox Code Playgroud)
但更有效的方法是使用compose(){purrr}构建一个只执行一次的函数,而不是重复mutate().
get_rsquared <- compose(as_mapper("r.squared"), summary, lm)
iris_n %>%
mutate(lm = map_dbl(data, ~ get_rsquared(Sepal.Length ~ Sepal.Width, data = .x)))
# A tibble: 3 x 3
Species data lm
<fct> <list> <dbl>
1 setosa <tibble [50 × 4]> 0.551
2 versicolor <tibble [50 × 4]> 0.277
3 virginica <tibble [50 × 4]> 0.209
Run Code Online (Sandbox Code Playgroud)
如果你知道你将一直使用Sepal.Length ~ Sepal.Width,你甚至可以预填lm()有partial():
pr_lm <- partial(lm, formula = Sepal.Length ~ Sepal.Width)
get_rsquared <- compose(as_mapper("r.squared"), summary, pr_lm)
iris_n %>%
mutate(lm = map_dbl(data, get_rsquared))
# A tibble: 3 x 3
Species data lm
<fct> <list> <dbl>
1 setosa <tibble [50 × 4]> 0.551
2 versicolor <tibble [50 × 4]> 0.277
3 virginica <tibble [50 × 4]> 0.209
Run Code Online (Sandbox Code Playgroud)
关于资源,我在{purrr}上写了一系列博文,你可以查一下:https://colinfay.me/tags/#purrr