我正在尝试将一个 tibble 转换为更长的格式。
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 = "_")
我遇到了以下错误:
Error: In row 3, can't recycle input of size 5 to size 0.
Run `rlang::last_error()` to see where the error occurred.
如何修复错误?
应包括将选择的列表列解除嵌套
unnest(where(is.list))
可能的解决方案还应包括将所有numeric(0)和NaN转换为NA。