如何使用 purrr::map() 改变列表列以存储通过 recipe() 创建的 "recipe" 对象?
How to mutate a list-column using purrr::map() to store a "recipe" object created via recipe()?
我正在尝试将 dplyr::mutate()
与 purrr::map()
结合使用来创建具有 recipes::recipe()
的“食谱”对象。
如果我在 tibble 上下文中这样做,效果很好:
library(rsample)
library(recipe)
iris_split <- initial_split(iris, prop = 0.6)
data_set_training <- training(iris_split)
recipe_standalone <- recipe(x = data_set_training, Species ~ .) # works
对比:
library(tibble)
library(dplyr)
library(purrr)
library(tidyr)
tibble(subset_training = data_set_training) %>%
nest(subset_training = subset_training) %>%
mutate(iris_recipe = map(.x = subset_training, .f = ~recipe(x = .x, Species ~ .))) # doesn't work
Error: Problem with mutate()
column iris_recipe
.
i iris_recipe = map(.x = subset_training, .f = ~recipe(x = .x, Species ~ .))
.
x object 'Species' not found
如何使用 map()
创建包含“食谱”对象的新列表列?
期望的输出
为了演示,我想得到这个:
desired_output <-
tibble(subset_training = list(data_set_training),
iris_recipe = list(recipe_standalone))
## # A tibble: 1 x 2
## subset_training iris_recipe
## <list> <list>
## 1 <df [90 x 5]> <recipe>
您已经按照嵌套的方式创建了时髦的结构。你已经把一个数据框作为一个列然后嵌套它,所以拉它,你实际上仍然有这个奇怪的 90x1
数据框列。
tibble(subset_training = data_set_training) %>%
nest(subset_training = subset_training) %>%
pull(subset_training) %>%
first()
#> # A tibble: 90 × 1
#> subset_training$Sepal.Length $Sepal.Width $Petal.Length $Petal.Width $Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 6.3 3.3 6 2.5 virgini…
#> 2 6 2.2 4 1 versico…
#> 3 5.7 2.8 4.5 1.3 versico…
#> 4 7.2 3.6 6.1 2.5 virgini…
#> 5 5 3.5 1.3 0.3 setosa
#> 6 5.1 3.8 1.6 0.2 setosa
#> 7 7.2 3.2 6 1.8 virgini…
#> 8 5.7 4.4 1.5 0.4 setosa
#> 9 4.4 2.9 1.4 0.2 setosa
#> 10 5.2 3.4 1.4 0.2 setosa
#> # … with 80 more rows
这是嵌套它的方式。
data_set_training %>%
nest(subset_training = everything()) %>%
pull(subset_training) %>%
first()
#> # A tibble: 90 × 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 6.3 3.3 6 2.5 virginica
#> 2 6 2.2 4 1 versicolor
#> 3 5.7 2.8 4.5 1.3 versicolor
#> 4 7.2 3.6 6.1 2.5 virginica
#> 5 5 3.5 1.3 0.3 setosa
#> 6 5.1 3.8 1.6 0.2 setosa
#> 7 7.2 3.2 6 1.8 virginica
#> 8 5.7 4.4 1.5 0.4 setosa
#> 9 4.4 2.9 1.4 0.2 setosa
#> 10 5.2 3.4 1.4 0.2 setosa
#> # … with 80 more rows
然后你会得到你要找的结果:
data_set_training %>%
nest(subset_training = everything()) %>%
mutate(iris_recipe = map(
.x = subset_training,
.f = ~recipe(x = .x, Species ~ .)
))
#> # A tibble: 1 × 2
#> subset_training iris_recipe
#> <list> <list>
#> 1 <tibble [90 × 5]> <recipe>
我正在尝试将 dplyr::mutate()
与 purrr::map()
结合使用来创建具有 recipes::recipe()
的“食谱”对象。
如果我在 tibble 上下文中这样做,效果很好:
library(rsample)
library(recipe)
iris_split <- initial_split(iris, prop = 0.6)
data_set_training <- training(iris_split)
recipe_standalone <- recipe(x = data_set_training, Species ~ .) # works
对比:
library(tibble)
library(dplyr)
library(purrr)
library(tidyr)
tibble(subset_training = data_set_training) %>%
nest(subset_training = subset_training) %>%
mutate(iris_recipe = map(.x = subset_training, .f = ~recipe(x = .x, Species ~ .))) # doesn't work
Error: Problem with
mutate()
columniris_recipe
.
iiris_recipe = map(.x = subset_training, .f = ~recipe(x = .x, Species ~ .))
.
x object 'Species' not found
如何使用 map()
创建包含“食谱”对象的新列表列?
期望的输出
为了演示,我想得到这个:
desired_output <-
tibble(subset_training = list(data_set_training),
iris_recipe = list(recipe_standalone))
## # A tibble: 1 x 2
## subset_training iris_recipe
## <list> <list>
## 1 <df [90 x 5]> <recipe>
您已经按照嵌套的方式创建了时髦的结构。你已经把一个数据框作为一个列然后嵌套它,所以拉它,你实际上仍然有这个奇怪的 90x1
数据框列。
tibble(subset_training = data_set_training) %>%
nest(subset_training = subset_training) %>%
pull(subset_training) %>%
first()
#> # A tibble: 90 × 1
#> subset_training$Sepal.Length $Sepal.Width $Petal.Length $Petal.Width $Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 6.3 3.3 6 2.5 virgini…
#> 2 6 2.2 4 1 versico…
#> 3 5.7 2.8 4.5 1.3 versico…
#> 4 7.2 3.6 6.1 2.5 virgini…
#> 5 5 3.5 1.3 0.3 setosa
#> 6 5.1 3.8 1.6 0.2 setosa
#> 7 7.2 3.2 6 1.8 virgini…
#> 8 5.7 4.4 1.5 0.4 setosa
#> 9 4.4 2.9 1.4 0.2 setosa
#> 10 5.2 3.4 1.4 0.2 setosa
#> # … with 80 more rows
这是嵌套它的方式。
data_set_training %>%
nest(subset_training = everything()) %>%
pull(subset_training) %>%
first()
#> # A tibble: 90 × 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 6.3 3.3 6 2.5 virginica
#> 2 6 2.2 4 1 versicolor
#> 3 5.7 2.8 4.5 1.3 versicolor
#> 4 7.2 3.6 6.1 2.5 virginica
#> 5 5 3.5 1.3 0.3 setosa
#> 6 5.1 3.8 1.6 0.2 setosa
#> 7 7.2 3.2 6 1.8 virginica
#> 8 5.7 4.4 1.5 0.4 setosa
#> 9 4.4 2.9 1.4 0.2 setosa
#> 10 5.2 3.4 1.4 0.2 setosa
#> # … with 80 more rows
然后你会得到你要找的结果:
data_set_training %>%
nest(subset_training = everything()) %>%
mutate(iris_recipe = map(
.x = subset_training,
.f = ~recipe(x = .x, Species ~ .)
))
#> # A tibble: 1 × 2
#> subset_training iris_recipe
#> <list> <list>
#> 1 <tibble [90 × 5]> <recipe>