Combine column to remove NA's

后端 未结 10 1647
野的像风
野的像风 2020-11-28 06:44

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

10条回答
  •  误落风尘
    2020-11-28 07:10

    If you want to stick with base,

    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))
    data[is.na(data)]<-","
    data$mycol<-paste0(data$x,data$y,data$z)
    data$mycol <- gsub(',','',data$mycol)
    

提交回复
热议问题