Column names after unnest() and unnest_longer()

48 Views Asked by At

I am working with a tibble which has a nested list of data frames containing column names that repeat column names of the tibble. I'm looking for the best way to unnest this tibble. The tibble has a structure like this:

df <- tibble(
  g = c(1, 2, 3),
  x = c("a", "b", "c"),
  data = list(
    data.frame(x = 1, y = 2),
    data.frame(x = 4:5, y = 6:7),
    data.frame(x = 10, y = 99)
  )
) 

Note that both df$x and df$data[[1]]$x exist.

I'm struggling to unnest and then rename or select columns by name.

df %>% unnest(cols = "data") fails due to the redundant names

df %>% unnest_longer(col = "data") works but

df %>% unnest_longer(col = "data") %>% select(`data$x`) fails: I can't select the column. I have similar issues when trying to rename, etc.

I'm not even sure what to call the data structure that results from df %>% unnest_longer(col = "data"):

df %>% unnest_longer(col = "data") %>% colnames() shows the colnames to be g,x, and data, but

df %>% unnest_longer(col = "data") %>% print() prints what looks like a data frame with four columns, even though it is a 4 x 3 tibble. What is this strange beast? And what's the right way to unnest? I guess this might be using names_repair in unnest but I can't see how to do it.

0

There are 0 best solutions below