使用group_by和nest的一个典型方法是估计一系列模型--
library(tidyverse)
mpg %>%
group_by(
manufacturer
) %>%
nest %>%
mutate(
mods = data %>%
map(
\(i)
lm(cty ~ displ, data = i)
)
)返回
# A tibble: 15 x 3
# Groups: manufacturer [15]
manufacturer data mods
<chr> <list> <list>
1 audi <tibble [18 x 10]> <lm>
2 chevrolet <tibble [19 x 10]> <lm>
3 dodge <tibble [37 x 10]> <lm>
4 ford <tibble [25 x 10]> <lm> 但是,试图通过使用nest_by来保持简洁会导致一个错误:
mpg %>%
nest_by(
manufacturer
) %>%
mutate(
mods = data %>%
map(
\(i)
lm(cty ~ displ, data = i)
)
)错误:
Error: Problem with `mutate()` column `mods`.
i `mods = data %>% map(function(i) lm(cty ~ displ, data = i))`.
x 'data' must be a data.frame, environment, or list
i The error occurred in row 1.如何使用nest_by复制group_by和nest的顺序使用
发布于 2021-06-15 19:17:39
我们可以在两者之间添加ungroup,因为nest_by返回与与map冲突的rowwise属性
library(dplyr)
library(purrr)
out1 <- mpg %>%
nest_by(
manufacturer
) %>%
ungroup %>%
mutate(
mods = data %>%
map(
\(i)
lm(cty ~ displ, data = i)
)
)-output
out1
# A tibble: 15 x 3
manufacturer data mods
<chr> <list<tibble[,10]>> <list>
1 audi [18 × 10] <lm>
2 chevrolet [19 × 10] <lm>
3 dodge [37 × 10] <lm>
4 ford [25 × 10] <lm>
5 honda [9 × 10] <lm>
6 hyundai [14 × 10] <lm>
7 jeep [8 × 10] <lm>
8 land rover [4 × 10] <lm>
9 lincoln [3 × 10] <lm>
10 mercury [4 × 10] <lm>
11 nissan [13 × 10] <lm>
12 pontiac [5 × 10] <lm>
13 subaru [14 × 10] <lm>
14 toyota [34 × 10] <lm>
15 volkswagen [27 × 10] <lm> 另外,当我们有nest_by时,就不需要map了。
out2 <- mpg %>%
nest_by(
manufacturer
) %>%
mutate(mods = list(lm(cty ~ displ, data = data)))-output
out2
# A tibble: 15 x 3
# Rowwise: manufacturer
manufacturer data mods
<chr> <list<tibble[,10]>> <list>
1 audi [18 × 10] <lm>
2 chevrolet [19 × 10] <lm>
3 dodge [37 × 10] <lm>
4 ford [25 × 10] <lm>
5 honda [9 × 10] <lm>
6 hyundai [14 × 10] <lm>
7 jeep [8 × 10] <lm>
8 land rover [4 × 10] <lm>
9 lincoln [3 × 10] <lm>
10 mercury [4 × 10] <lm>
11 nissan [13 × 10] <lm>
12 pontiac [5 × 10] <lm>
13 subaru [14 × 10] <lm>
14 toyota [34 × 10] <lm>
15 volkswagen [27 × 10] <lm> 除了call参数之外,输出都是相同的。
out1$mods[[1]]
Call:
lm(formula = cty ~ displ, data = i)
Coefficients:
(Intercept) displ
22.066 -1.751
> out2$mods[[1]]
Call:
lm(formula = cty ~ displ, data = data)
Coefficients:
(Intercept) displ
22.066 -1.751
> all.equal(out1$mods[[1]], out2$mods[[1]], check.attributes = FALSE)
[1] "Component “call”: target, current do not match when deparsed"https://stackoverflow.com/questions/67992206
复制相似问题