I'm trying to use tidyr::pivot_longer() function in a tibble, and it does not work. Some of my data set columns are imported as logical, so I have to convert them to integer columns. When I try to use pivot_longer(), the results are wrong. Here is an example of my problem:
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])")