merge/combine columns with same name but incomplete data

后端 未结 7 973
臣服心动
臣服心动 2020-12-14 17:57

I have two data frames that have some columns with the same names and others with different names. The data frames look something like this:

df1
      ID hel         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 18:24

    @ananda-mahto 's answer is more elegant but here is my suggestion:

    library(reshape2)
    df1=melt(df1,id='ID',na.rm=TRUE)
    df2=melt(df2,id='ID',na.rm=TRUE)
    DF=rbind(df1,df2)
    # Not needeed,  added na.rm=TRUE based on @ananda-mahto's valid comment
    # DF<-DF[!is.na(DF$value),]
    dcast(DF,ID~variable,value.var='value')
    

提交回复
热议问题