r programming --- merge function returns column names with .x and .y

If every column name is repeated, you can just use

merge(x = small, y = med.large, by = names(small), all.x=T)

If column names differ, you can build a vector of names in both with

intersect(names(small), names(med.large))

and pass that to by. Otherwise, if the two data.frames share a column that is not passed to by, you'll end up with .x or .y suffixes.


After the merge (and in the case you dont have column names that otherwise include .x or .y) you can replace .x and .y by '':

   colnames(merged_df) <- gsub('.x','',names(merged_df))
   colnames(merged_df) <- gsub('.y','',names(merged_df))

Tags:

R