Combine column to remove NA's

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

    You can use unlist to turn the columns into one vector. Afterwards, na.omit can be used to remove the NAs.

    cbind(data[1], mycol = na.omit(unlist(data[-1])))
    
       a mycol
    x1 A     1
    x2 B     2
    y3 C     3
    z4 D     4
    z5 E     5
    

提交回复
热议问题