Unnest() behavior with list column of character vectors

tidyr

#1

I’m seeing some strange behavior trying to use tidyr::unnest() on a data frame that was created from jsonlite::stream_in(). I read in the data from a JSON file and in the file the labels field is an array of string values, so I understand that it is being parsed in as an atomic vector. I’m getting the following message when I try to unnest:

Error: Each column must either be a list of vectors or a list of data frames [labels]

As far as I can see the labels column is a list of character vectors:

> transactions
# A tibble: 37,057 x 6
   rowkey                   price quantity animalRowKey   labels    transactionDay
 * <chr>                    <dbl>    <dbl> <chr>          <list>    <chr>         
 1 13652:1:SMIWY:112301:4:0 15.2      1.00 13652:1:SMIWY7 <chr [1]> 2010-08-05    
 2 13652:1:SMIWY:112369:3:0 18.0      1.00 13652:1:SMIWY7 <chr [1]> 2010-08-07    
 3 13652:1:SMIWY:113107:7:0 89.4      1.00 13652:1:SMIWY7 <chr [1]> 2010-08-31    
 4 13652:1:SMIWY:115041:2:0 28.8      1.00 13652:1:SMIWY7 <chr [2]> 2010-11-01    
 5 13652:1:SMIWY:119353:4:0  7.00     1.00 13652:1:SMIWY7 <chr [1]> 2011-04-05    

but when I get this:

> transactions_cleaned <- transactions %>%  unnest()
Error: Each column must either be a list of vectors or a list of data frames [labels]

if it’s not a list of vectors, how can I figure out what type of object is in that column?


#2

Maybe at least one of your labels items is NULL – that seems to cause that error:

suppressPackageStartupMessages(library(tidyverse))

tbl <- tribble(
  ~ ID, ~ Num, ~ Labels,
  "X", 5, c("abc", "def"),
  "Y", 3, NULL,
  "Z", 10, c("jk", "elmeno-p")
)

tbl
#> # A tibble: 3 x 3
#>      ID   Num    Labels
#>   <chr> <dbl>    <list>
#> 1     X     5 <chr [2]>
#> 2     Y     3    <NULL>
#> 3     Z    10 <chr [2]>

tbl %>% unnest()
#> Error: Each column must either be a list of vectors or a list of data frames [Labels]

Try this code:

map_chr(transactions$labels, typeof) %>% table()

That should let you know if you have any non-character items in that column.


#3

Yes, that is the problem. Thank you so much. That has been driving me crazy all morning!