This question already has an answer here:
- Combine column to remove NA's 9 answers
I have a data frame
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,NA))
It looks like this:
a x y z
1 A 1 NA NA
2 B 2 NA NA
3 C NA 3 NA
4 D NA NA 4
5 E NA NA NA
I expect to get a data like this:
a N
1 A 1
2 B 2
3 C 3
4 D 4
5 E NA
Thank you!
A dplyr solution using
coalesce
.No need for
select
withtransmute
:you may want to try something like this:
pmax
seems to suggest itself here, which should be substantially quicker on large data compared to looping over each row: