0

我正在尝试与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

错误:mutate()列有问题iris_recipe
iris_recipe = map(.x = subset_training, .f = ~recipe(x = .x, Species ~ .))
未找到 x 对象“物种”


如何使用map()创建包含“recipe”对象的新列表列?

所需的输出
为了演示,我想得到这个:

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>   
4

1 回答 1

3

您以嵌套的方式创建了时髦的结构。你已经把一个数据框作为一列然后嵌套它,所以拉它,你实际上仍然有这个奇怪的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>
于 2021-12-15T13:55:25.537 回答