I'm trying to convert a tibble to a longer format.
library(tidyverse)
tbl <- tibble(
a_X_values = list(numeric(0), c(3, NaN), c(4, 5, 6, NaN, NaN)),
a_Y_values = list(5, NaN, numeric(0)),
b_X_values = c(7, 8, 9),
b_Y_values = c(NA, NaN, 1)
)
tbl_longer <- tbl %>%
unnest(where(is.list)) %>%
pivot_longer(cols = everything(),
names_to = c("alpha", "beta", ".value"), names_sep = "_")
I am getting the following error:
Error: In row 3, can't recycle input of size 5 to size 0.
Run `rlang::last_error()` to see where the error occurred.
How do I fix the error?
It should involve unnesting selected columns which are lists
unnest(where(is.list))A possible solution should also include converting all numeric(0) and NaN to NA.