[英]How to build a named list from tibble list columns?
是否有一種直接的、管道友好的方法可以從包含列表列的小標題生成list
object ?
考慮下面的例子。 如果my_starwars
是給定的tibble ,我怎樣才能獲得像desired_output
這樣的命名列表?
library(dplyr, warn.conflicts = FALSE)
library(purrr)
my_starwars <-
starwars %>%
slice(1)
films <- my_starwars %>% pull(films) %>% set_names("films")
vehicles <- my_starwars %>% pull(vehicles) %>% set_names("vehicles")
desired_output <- c(films, vehicles)
desired_output
#> $films
#> [1] "The Empire Strikes Back" "Revenge of the Sith"
#> [3] "Return of the Jedi" "A New Hope"
#> [5] "The Force Awakens"
#>
#> $vehicles
#> [1] "Snowspeeder" "Imperial Speeder Bike"
雖然上面的代碼可以完成這項工作,但我想要一些來自 pipe 的更少的手動操作,例如:
my_starwars %>%
build_list(films, vehicles, foo, bar) # this is a demo
## $films
## [1] "The Empire Strikes Back" "Revenge of the Sith" "Return of the Jedi" "A New Hope" ## "The Force Awakens"
## $vehicles
## [1] "Snowspeeder" "Imperial Speeder Bike"
## $foo
## [1] "foo" "foo" "foo"
## $bar
## [1] "bar" "bar"
使用tidyverse
工具是否有一種簡單的方法來做到這一點?
編輯
盡管@GuedesBF 的解決方案非常適合最初的my_starwars
示例,但它因使用不同類型的 tibble 而失敗。 所以我原來的例子太小了,不能代表我的問題。 請考慮以下示例,其中trb
是一個包含 2 個列表列的 tibble,每個列都有一個recipe
object。
library(recipes)
car_rec_1 <-
recipe(mpg ~ ., data = mtcars) %>%
step_ns(disp, deg_free = 5)
car_rec_2 <-
recipe(mpg ~ ., data = mtcars) %>%
step_dummy(am)
trb <-
tribble(~car_rec_1, ~car_rec_2,
car_rec_1, car_rec_2)
undesired_output_trb <-
trb %>%
lapply(unlist)
car_rec_1 <- trb %>% pull(car_rec_1) %>% setNames("car_rec_1")
car_rec_2 <- trb %>% pull(car_rec_2) %>% setNames("car_rec_2")
desired_output_trb <- c(car_rec_1, car_rec_2)
當我們使用@GuedesBF 的解決方案時,我們得到了undesired_output_trb
,而我想要的是desired_output_trb
。 如何做到這一點?
我們可以循環應用 function 到unlist()
select
ed列。
my_starwars %>%
select(c(films, vehicles)) %>%
lapply(unlist)
#Or if tidyverse is a must, we can replace `lapply` with `purrr::map`
my_starwars %>%
select(c(films, vehicles)) %>%
map(unlist)
$films
[1] "The Empire Strikes Back" "Revenge of the Sith" "Return of the Jedi" "A New Hope" "The Force Awakens"
$vehicles
[1] "Snowspeeder" "Imperial Speeder Bike"
基於這個答案,另一個解決方案是tidyr::pivot_longer()
和tibble::deframe()
:
library(dplyr, warn.conflicts = FALSE)
library(tibble)
library(tidyr)
my_starwars <-
starwars %>%
slice(1)
my_starwars %>%
select(films, vehicles) %>%
pivot_longer(everything()) %>%
deframe()
#> $films
#> [1] "The Empire Strikes Back" "Revenge of the Sith"
#> [3] "Return of the Jedi" "A New Hope"
#> [5] "The Force Awakens"
#>
#> $vehicles
#> [1] "Snowspeeder" "Imperial Speeder Bike"
library(recipes)
car_rec_1 <-
recipe(mpg ~ ., data = mtcars) %>%
step_ns(disp, deg_free = 5)
car_rec_2 <-
recipe(mpg ~ ., data = mtcars) %>%
step_dummy(am)
trb <-
tribble(~car_rec_1, ~car_rec_2,
car_rec_1, car_rec_2)
trb %>%
pivot_longer(everything()) %>%
deframe()
#> $car_rec_1
#> Recipe
#>
#> Inputs:
#>
#> role #variables
#> outcome 1
#> predictor 10
#>
#> Operations:
#>
#> Natural Splines on disp
#>
#> $car_rec_2
#> Recipe
#>
#> Inputs:
#>
#> role #variables
#> outcome 1
#> predictor 10
#>
#> Operations:
#>
#> Dummy variables from am
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.