【发布时间】:2021-07-26 07:58:53
【问题描述】:
我正在尝试在 tibble 中使用 tidyr::pivot_longer() 函数,但它不起作用。我的一些数据集列是作为逻辑导入的,所以我必须将它们转换为整数列。当我尝试使用 pivot_longer() 时,结果是错误的。这是我的问题的一个例子:
test <- tibble(name = paste0("TEST",1:5),
acl.1 = 1:5,
acl.2 = 11:15,
acl.3 = rep(NA,5),
mcl.1 = 6:10,
mcl.2 = 16:20,
mcl.3 = rep(NA,5)
)
test <- test %>% mutate(across(where(is.logical), as.integer)) # trying to convert from logical to integer
test[is.na(test)] <- 0 # trying to replace NA's with 0
testLong <- test %>%
pivot_longer(cols = c(starts_with("acl."), starts_with("mcl.")),
names_to = c(".value","label"),
names_pattern = "(....)([1:3])")
【问题讨论】: