Home > Blockchain >  How to rotate/transpose a tibble with list-columns
How to rotate/transpose a tibble with list-columns

Time:12-31

I'm trying to "rotate" to tibble that contains list-columns. In the example below, I'm trying to get from trb_1 to trb_2.

library(recipes)
library(tibble)

# I have to "recipes" that I organize in a tibble.

# car_rec_1
recipe(mpg ~ ., data = mtcars) %>% step_ns(disp, deg_free = 5)
# car_rec_2
recipe(mpg ~ ., data = mtcars) %>% step_dummy(am) 


trb_1 <-
  tribble(~car_rec_1,                                                     ~car_rec_2,
        recipe(mpg ~ ., data = mtcars) %>% step_ns(disp, deg_free = 5),   recipe(mpg ~ ., data = mtcars) %>% step_dummy(am) 
        )

trb_2 <-
  tribble(~name, ~value,
          "car_rec_1", recipe(mpg ~ ., data = mtcars) %>% step_ns(disp, deg_free = 5),
          "car_rec_2", recipe(mpg ~ ., data = mtcars) %>% step_dummy(am)
          )


trb_1
#> # A tibble: 1 x 2
#>   car_rec_1 car_rec_2
#>   <list>    <list>   
#> 1 <recipe>  <recipe>
trb_2
#> # A tibble: 2 x 2
#>   name      value   
#>   <chr>     <list>  
#> 1 car_rec_1 <recipe>
#> 2 car_rec_2 <recipe>

I've tried

library(sjmisc)

rotate_df(trb_1)

But the output is not as desired (i.e. unlike the desired trb_2)

CodePudding user response:

Maybe

trb_2 <- trb_1 %>% 
  pivot_longer(everything())

-Outcome

> trb_2
# A tibble: 2 x 2
  name      value   
  <chr>     <list>  
1 car_rec_1 <recipe>
2 car_rec_2 <recipe>
  • Related