Merging two columns into one in R

后端 未结 7 919
情深已故
情深已故 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:25

    Another very simple solution in this case is to use the rowSums function.

    df$New<-rowSums(df[, c("A", "B")], na.rm=T)
    df<-df[, c("ID", "New")]
    

    Update: Thanks @Artem Klevtsov for mentioning that this method only works with numeric data.

提交回复
热议问题