Below is a tibble. I wish to unnest it. The desired tibble should contain 16 rows.
- The first row should expand to 4 rows
- The second row should expand to 2 rows
- The third row should expand to 5*2 rows
The problem here is that in the third row, we have a vector of size 5 and a vector of size 2. I want that row to expand to 5 * 2 = 10 rows.
library(tidyverse)
tbl <- tibble(
a_X_values = list(NA, c(3, NA), c(4, 5, 6, NaN, NaN)),
a_Y_values = list(c(NaN, NaN, 3, 5), NaN, NaN),
b_X_values = list(7, 8, c(3, NaN)),
b_Y_values = c(NA, NaN, 1)
)
tbl1 <- tbl %>%
unnest(where(is.list))
The desired output would be
tbl_desired <- tibble(
a_X_values = c(NA, NA, NA, NA, 3, NA, 4, 5, 6, NaN, NaN, 4, 5, 6, NaN, NaN ),
a_Y_values = c(NaN, NaN, 3, 5, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN),
b_X_values = c(7, 7, 7, 7, 8, 8, 3, NaN, 3, NaN, 3, NaN, 3, NaN, 3, NaN),
b_Y_values = c(NA, NA, NA, NA, NaN, NaN, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
)