Combine column with NA's [duplicate]

陌路散爱 提交于 2019-12-11 04:17:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!