Merging two columns into one in R

后端 未结 7 908
情深已故
情深已故 2020-11-29 02:06

I have the following data frame, and am trying to merge the two columns into one, while replacing NA\'s with the numeric values.

ID    A     B
1         


        
7条回答
  •  孤城傲影
    2020-11-29 02:43

    This question's been around for a while, but just to add another possible approach that does not depend on any libraries:

    df$new = t(df[-1])[!is.na(t(df[-1]))]
    
    #   ID  A  B new
    # 1  1  3 NA   3
    # 2  2 NA  2   2
    # 3  3 NA  4   4
    # 4  4  1 NA   1
    

提交回复
热议问题