I have converted a JSON table into a data frame with nested columns. I want to unnest one of these columns, but I am getting an error when that nested column holds an empty list (but not when that nested column holds an empty data frame).
For example, given this data, I see three kinds of rows:
test_data <- structure(list(duration = c(2624L, 3374L, 901L), precincts = list(
structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"),
structure(list(smartzoneName = c("Loc 1", "Loc 2",
"Loc 1", "Loc 2", "Loc 1"), duration = c(264L,
1329L, 968L, 474L, 359L), timestamp = c(1545522428000, 1545522692000,
1545524021000, 1545524989000, 1545525463000), smartzoneId = c(275L,
251L, 275L, 251L, 275L)), class = "data.frame", row.names = c(NA,
5L)), list())), row.names = c(NA, -3L), class = "data.frame")
test_data
# duration precincts
# <int> <list>
# 2624 <data.frame [0 × 0]>
# 3374 <data.frame [5 × 4]>
# 901 <list [0]>
I'd like to unnest based on precinct. But the empty list rows create an error:
test_data %>%
slice(1:2) %>%
unnest(precincts, .id = "id")
# Runs fine, dropping first row of original data
test_data %>%
unnest(precincts, .id = "id")
# Error: Each column must either be a list of vectors or a list of data frames [precincts]
Is there a way to convert the empty lists to empty data tables, or filter out both kinds of rows (or at least the problematic ones) before the unnest? Is this expected behavior, or is this a bug with tidyr? (using 0.8.2)