combine_cols<- function(primary,secondary,linker,column) {
require(data.table)
a<-data.table("Sample"=primary[,linker], primary[,column])
b<-data.table("Sample"=secondary[,linker], secondary[,column])
c <- merge(a, b, by = "Sample", all=TRUE)
c[,Status := ifelse(!is.na(c[,paste0(column,".x")]), paste0(column,".x"),
paste0(column,".y"))]
c[,`:=` (paste0(column,".x")=NULL, paste0(column,".y")= NULL)]
return(c)
}
mydata1<-data.frame("Sample"=c("100","101","102","103"),"Status"=c("Y","","","partial"))
mydata2<-data.frame("Sample"=c("100","101","102","103","106"),"Status"=c("NA","Y","","","Y"))
print((combine_cols(mydata1,mydata2,"Sample",c("Status"))))
I'm trying to create a function to merge columns of split data. The ifelse
line isn't working because the paste0(column,".x")
is recognized as a character and not a column name. How can I ensure that c[,paste0(column,".x")]
reflects c$c[,paste0(column,".x")]
? Better yet, how can I modify this line to handle a list of column names?