Skip to content
Prev 319257 / 398503 Next

merge function while obviating duplicate columns XXXX

You can use the set-oriented functions setdiff(), union(), and intersect().
E.g., setdiff(colnames(data2), colnames(data1)) gives the names of columns
of data2 that are not  names of columns of data1.  The following might be
what you want
    merge(data1, data2[, c("id", setdiff(colnames(data2),colnames(data1)))], by="id")
You didn't give an example of the data nor the desired result so I made some up:
   > data1 <- data.frame(id=c(1,1,2,3), Name=c("Joe","Joe","Ken","Leo"))
   > data2 <- data.frame(id=c(2,3), Name=c("Melody","Nell"), Age=c(45,49))
   > merge(data1, data2, by="id")
     id Name.x Name.y Age
   1  2    Ken Melody  45
   2  3    Leo   Nell  49
   > merge(data1, data2[, c("id", setdiff(colnames(data2),colnames(data1)))], by="id")
     id Name Age
   1  2  Ken  45
   2  3  Leo  49

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com