How to change a tibble of lists to normal columns and preserve the column names

340 views Asked by At

I have the following data:

dat <- structure(list(description = list("MedianTime_SpeederTemp", "MedianTime_SpeederTemp"),
                      variables   = list(list(src = "y", shown = TRUE),
                                         list(src = "x", shown = FALSE)),
                      id          = list(13190L, 14190L)),
                 row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"))

# A tibble: 2 x 3
  description variables        id       
  <list>      <list>           <list>   
1 <chr [1]>   <named list [2]> <int [1]>
2 <chr [1]>   <named list [2]> <int [1]>

As you can see the data contains three list columns. The second list column is a nested list.

So what I want to do now is:

  1. unnest the second column "variables" which I know I can do with

    dat %>% unnest_wider(variables)

  2. Unlist the other two columns so that they contain the "true" data format, i.e. the character string for "description" and the number stored in "id" while preserving the column names. And here I struggle. I also tried unnest_wider on the "description column, but it changed the column name to "...1", but I want to keep it as "description". I know I could probably manually change this afterwards, but my real data has hundreds of these columns, so that's not an option.

Any ideas?

0

There are 0 answers