A chr column has vectors and atomic values. How can I turn those vectors into longer pivoted rows?

All the searches online seem to be 'vectors -> columns' instead of 'vectors -> rows' and the problem is that there are too many unique values in the vectors and rows, resulting in a massive matrix that grows too much, essentially nrow() per unique string.

library(tidytable)
df_example <- tidytable(a = list(c('a','b'),
                                 character(0),
                                 "z",
                                 c('y','z')),
                        id = list('aa', 'bb', 'cc', 'cc'))

In the above example, there are all types of values in column a. I don't have any values akin to c('d') in that column (it would just be 'd').

What I would like is to essentially pivot_longer() those vectors into multiple rows, similar to the following:

df_example <- tidytable(a = list('a', 'b',
                                 character(0),
                                 "z",
                                 'y', 'z'),
                        id = list('aa', 'aa', 'bb', 'cc', 'cc', 'cc'))

How would I be able to achieve this?

It's not a real pivot, since these are not separate columns. I would say what you have is a nested dataframe, so you can unnest() it. Then you loose the fact that you a is a list-column (is that really a good idea anyway?)

I don't have tidytable, I expect it's supposed to work similarly to a tibble:

library(tidyverse) |> suppressPackageStartupMessages()


df_example <- tibble(a = list(c('a','b'),
                                 character(0),
                                 "z",
                                 c('y','z')),
                        id = list('aa', 'bb', 'cc', 'cc'))


df_example |>
  unnest(cols = a,keep_empty = TRUE)
#> # A tibble: 6 × 2
#>   a     id       
#>   <chr> <list>   
#> 1 a     <chr [1]>
#> 2 b     <chr [1]>
#> 3 <NA>  <chr [1]>
#> 4 z     <chr [1]>
#> 5 y     <chr [1]>
#> 6 z     <chr [1]>

Created on 2023-11-27 with reprex v2.0.2

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.