I have some columns in R and for each row there will only ever be a value in one of them, the rest will be NA\'s. I want to combine these into one column with the non-NA val
A dplyr::coalesce based solution could be as:
dplyr::coalesce
data %>% mutate(mycol = coalesce(x,y,z)) %>% select(a, mycol) # a mycol # 1 A 1 # 2 B 2 # 3 C 3 # 4 D 4 # 5 E 5
Data
data <- data.frame('a' = c('A','B','C','D','E'), 'x' = c(1,2,NA,NA,NA), 'y' = c(NA,NA,3,NA,NA), 'z' = c(NA,NA,NA,4,5))