Combine column to remove NA's

后端 未结 10 1661
野的像风
野的像风 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:01

    Something like this ?

    data.frame(a=data$a, mycol=apply(data[,-1],1,sum,na.rm=TRUE))
    

    gives :

      a mycol
    1 A     1
    2 B     2
    3 C     3
    4 D     4
    5 E     5
    

提交回复
热议问题