在保留组的同时映射 tibble 列
map over tibble columns while preserving groups
我有一个分组的小标题。我想使用 map()
遍历 tibble 的列。在每一列中,我希望 map()
对每个组分别采取行动。换句话说,我希望 map()
尊重 tibble 的分组结构。
但是 map()
似乎不尊重 tibbles 的分组结构。这是一个最小的例子:
library(dplyr)
library(purrr)
data(iris)
iris %>%
group_by(Species) %>%
map(length)
在鸢尾花数据集中,共有三个物种和四列(不包括“物种”)。因此,我希望 map()
到 return 一个 3 × 4 = 12 长度的列表,或者 return 一个总共有 12 个长度的嵌套列表。但它 return 是一个包含 5 个元素的列表:每列一个,计算分组列。这五个元素中的每一个都是一列的总长度 (150)。我怎样才能调整上面的代码来提供我想要的结果?
在这个最小的示例中,使用 map()
的令人满意的替代方法是
iris %>%
group_by(Species) %>%
summarize(
mutate(across(everything(), length))
)
哪个return
# A tibble: 3 x 5
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
* <fct> <int> <int> <int> <int>
1 setosa 50 50 50 50
2 versicolor 50 50 50 50
3 virginica 50 50 50 50
但在大多数情况下,这种替代方法行不通。问题是我通常需要 summarize()
和 mutate
到 return loess()
objects,而不是整数。当我试图让他们达到 return loess()
objects 时,他们会因为
这样的错误而窒息
Error: Problem with `summarise()` input `..1`.
x Input must be a vector, not a `loess` object.
do
允许您一次在一组上工作
编辑:如您所说,do
已被取代,这是更直接(和鼓励)的做法。 (我在 do
回答之前尝试过的问题是我没有使用 cur_data()
。)
colnms <- names(iris)[2:4]
colnms
# [1] "Sepal.Width" "Petal.Length" "Petal.Width"
iris %>%
group_by(Species) %>%
summarize(
other = colnms,
mdl = map(colnms, ~ loess(as.formula(paste("Sepal.Length ~", .x)),
data = cur_data()))
)
# Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
# pseudoinverse used at 0.0975
# Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
# neighborhood radius 0.2025
# Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
# reciprocal condition number 2.8298e-016
# Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
# There are other near singularities as well. 0.01
# # A tibble: 9 x 3
# # Groups: Species [3]
# Species other mdl
# <fct> <chr> <list>
# 1 setosa Sepal.Width <loess>
# 2 setosa Petal.Length <loess>
# 3 setosa Petal.Width <loess>
# 4 versicolor Sepal.Width <loess>
# 5 versicolor Petal.Length <loess>
# 6 versicolor Petal.Width <loess>
# 7 virginica Sepal.Width <loess>
# 8 virginica Petal.Length <loess>
# 9 virginica Petal.Width <loess>
我有一个分组的小标题。我想使用 map()
遍历 tibble 的列。在每一列中,我希望 map()
对每个组分别采取行动。换句话说,我希望 map()
尊重 tibble 的分组结构。
但是 map()
似乎不尊重 tibbles 的分组结构。这是一个最小的例子:
library(dplyr)
library(purrr)
data(iris)
iris %>%
group_by(Species) %>%
map(length)
在鸢尾花数据集中,共有三个物种和四列(不包括“物种”)。因此,我希望 map()
到 return 一个 3 × 4 = 12 长度的列表,或者 return 一个总共有 12 个长度的嵌套列表。但它 return 是一个包含 5 个元素的列表:每列一个,计算分组列。这五个元素中的每一个都是一列的总长度 (150)。我怎样才能调整上面的代码来提供我想要的结果?
在这个最小的示例中,使用 map()
的令人满意的替代方法是
iris %>%
group_by(Species) %>%
summarize(
mutate(across(everything(), length))
)
哪个return
# A tibble: 3 x 5
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
* <fct> <int> <int> <int> <int>
1 setosa 50 50 50 50
2 versicolor 50 50 50 50
3 virginica 50 50 50 50
但在大多数情况下,这种替代方法行不通。问题是我通常需要 summarize()
和 mutate
到 return loess()
objects,而不是整数。当我试图让他们达到 return loess()
objects 时,他们会因为
Error: Problem with `summarise()` input `..1`.
x Input must be a vector, not a `loess` object.
do
允许您一次在一组上工作
编辑:如您所说,do
已被取代,这是更直接(和鼓励)的做法。 (我在 do
回答之前尝试过的问题是我没有使用 cur_data()
。)
colnms <- names(iris)[2:4]
colnms
# [1] "Sepal.Width" "Petal.Length" "Petal.Width"
iris %>%
group_by(Species) %>%
summarize(
other = colnms,
mdl = map(colnms, ~ loess(as.formula(paste("Sepal.Length ~", .x)),
data = cur_data()))
)
# Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
# pseudoinverse used at 0.0975
# Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
# neighborhood radius 0.2025
# Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
# reciprocal condition number 2.8298e-016
# Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric, :
# There are other near singularities as well. 0.01
# # A tibble: 9 x 3
# # Groups: Species [3]
# Species other mdl
# <fct> <chr> <list>
# 1 setosa Sepal.Width <loess>
# 2 setosa Petal.Length <loess>
# 3 setosa Petal.Width <loess>
# 4 versicolor Sepal.Width <loess>
# 5 versicolor Petal.Length <loess>
# 6 versicolor Petal.Width <loess>
# 7 virginica Sepal.Width <loess>
# 8 virginica Petal.Length <loess>
# 9 virginica Petal.Width <loess>