Combine column with NA's [duplicate]

2019-07-25 11:25发布

This question already has an answer here:

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!

3条回答
太酷不给撩
2楼-- · 2019-07-25 11:57

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))
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-25 12:17

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
查看更多
叛逆
4楼-- · 2019-07-25 12:17

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
查看更多
登录 后发表回答