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

dplyr How to `reframe` all the columns in a dataframe?

Hello I would like to reframe all the columns in a tibble that contains all list columns without having to specify all the columns by name. Below contains an example. my_data is all list columns and I want to turn it into a regular dataframe using reframe. I can do this by specifying all the names of the columns.

library(dplyr)
my_data <- tibble(C1 = list(c(1,2,3,4)),
       C2 = list(c(5,6,7,8)),
       C3 = list(c(9,10,11,12)))

my_data |> 
  rowwise() |> 
  reframe(C1, C2, C3) #The output I would like to get
#> # A tibble: 4 × 3
#>      C1    C2    C3
#>   <dbl> <dbl> <dbl>
#> 1     1     5     9
#> 2     2     6    10
#> 3     3     7    11
#> 4     4     8    12

my_data |> 
  rowwise() |> 
  reframe(everything()) #Would like to find a way to reframe all the columns
#> Error in `reframe()`:
#> ℹ In argument: `everything()`.
#> ℹ In row 1.
#> Caused by error:
#> ! `everything()` must be used within a *selecting* function.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-selection-context.html> for
#>   details.

>Solution :

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

Instead of dplyr::reframe you could use tidyr::unnest_longer like so:

library(tidyr)

my_data |>
  tidyr::unnest_longer(everything())
#> # A tibble: 4 × 3
#>      C1    C2    C3
#>   <dbl> <dbl> <dbl>
#> 1     1     5     9
#> 2     2     6    10
#> 3     3     7    11
#> 4     4     8    12
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