Merging two data frames sharing columns with the same name, those columns will receive .x
and .y
suffixes for the first and the second merged data frame.
Follow this example to renew those columns.
(test <- data.frame(a = 1:5, b.x = 6:10, b.y = 11:15, c.x = 16:20, c.y = 21:25))
a b.x b.y c.x c.y
1 1 6 11 16 21
2 2 7 12 17 22
3 3 8 13 18 23
4 4 9 14 19 24
5 5 10 15 20 25
colnames(test)[grepl("\\.x", colnames(test))] <- substr(colnames(test)[grepl("\\.x", colnames(test))], 1, nchar(colnames(test)[grepl("\\.x", colnames(test))])-2)
colnames(test)[grepl("\\.y", colnames(test))] <- paste0(substr(colnames(test)[grepl("\\.y", colnames(test))], 1, nchar(colnames(test)[grepl("\\.y", colnames(test))])-2), ".new")
colnames(test)
[1] "a" "b" "b.new" "c" "c.new"