问题
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!
回答1:
A dplyr solution using coalesce
.
library(dplyr)
data %>%
mutate(N = coalesce(x, y, z)) %>%
select(a, N)
a N
1 A 1
2 B 2
3 C 3
4 D 4
5 E NA
No need for select
with transmute
:
data %>%
transmute(a, N = coalesce(x, y, z))
回答2:
you may want to try something like this:
> result <- apply(data[, -1], 1, function(x) ifelse(all(is.na(x)), NA, x[!is.na(x)]))
> data.frame(a=data[,1], N=result)
a N
1 A 1
2 B 2
3 C 3
4 D 4
5 E NA
回答3:
pmax
seems to suggest itself here, which should be substantially quicker on large data compared to looping over each row:
do.call(pmax, c(data[c("x","y","z")],na.rm=TRUE) )
#[1] 1 2 3 4 NA
cbind(data["a"], N=do.call(pmax, c(data[c("x","y","z")],na.rm=TRUE) ))
# a N
#1 A 1
#2 B 2
#3 C 3
#4 D 4
#5 E NA
来源:https://stackoverflow.com/questions/39237123/combine-column-with-nas