Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to rotate/transpose a tibble with list-columns

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading