rdataframetidyrunnest

Unnesting lists inside a column using unnest_longer()


I have this code:

library(jsonlite)
library(tidyverse)
    datasets.raw <- fromJSON(query, flatten = TRUE, simplifyDataFrame = TRUE)
datasets.raw$fields.species
datasets.df <- do.call(rbind, datasets.raw) #convert to dataframe

alternatively, the data could be obtained like this:

datasets.df <- structure(list(id = c("221", "PXD011681", "PXD013748", "PXD017277", 
                              "PXD013449", "PXD017613"),
                              source = c("221", "pride", "pride", "pride", "pride", "pride"), 
                              fields.species = list(221L, "Homo Sapiens (human)", c("Passer Hispaniolensis", "Passer Domesticus Domesticus"                                                                                                                       ), "Bifidobacterium Longum Subsp. Longum", "Homo Sapiens (human)", 
                                                                                                                      "Homo Sapiens (human)")), 
                              row.names = c("hitCount", "entries.1", 
                                                                                                                                                              "entries.2", "entries.3", "entries.4", "entries.5"), class = "data.frame")

I would like to unpack the lists inside the columns, such as the one in datasets.df$fields.species, and convert them into individual rows. FWIK, unnest_longer() should be ideal for this. I've tried:

unpack <- datasets.df %>% unnest_longer(fields.species)

but this gives me an error:

! Can't combine `..1$fields.species` <integer> and `..3$fields.species` <character>.

Any idea why this does not work?


Solution

  • (unpack <- datasets.df %>%
      rowwise() |>
      mutate(
        fields.species =
          list(as.character(fields.species))) |>
      unnest_longer(fields.species))