Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Suffixes in Merge to All Non-by Columns

suffixes in merge works only on common column names. Is there anyway to extend this to the rest of the columns as well without manually updating columns before the merge?

That is -

df1 <- data.table(
a = c(1,2,3,4,5,6),
b = c('a','b','f','e','r','h'),
d = c('q','l','o','n','q','z')
)

df2 <- data.table(
a = c(1,2,3,4,5,6),
d = c('q','l','o','n','q','z')
)

colnames(merge(df1,df2, by = 'a', suffixes = c("1","2")))
#[1] "a"  "b"  "d1" "d2" what it does
#[1] "a"  "b1" "d1" "d2" what I'd like it to do

The current way I'm handling this resembles @mrip's answer.

df1 <- data.table(
a = c(1,2,3,4,5,6),
b = c('a','b','f','e','r','h'),
r = c('a','b','f','e','r','h'),
d = c('q','l','o','n','q','z')
)

df2 <- data.table(
a = c(1,2,3,4,5,6),
c = c('a','b','f','e','r','h'),
q = c('a','b','f','e','r','h'),
d = c('q','l','o','n','q','z')
)

dfmerge <- (merge(df1,df2, by = c("a"), suffixes = c("1","2")))

setnames(
dfmerge,
setdiff(names(df1),names(df2)),
paste0(setdiff(names(df1),names(df2)),"1")
)

setnames(
dfmerge,
setdiff(names(df2),names(df1)),
paste0(setdiff(names(df2),names(df1)),"2")
)

colnames(dfmerge)
#[1] "a"  "b1" "r1" "d1" "c2" "q2" "d2"
like image 476
TheComeOnMan Avatar asked Oct 02 '13 16:10

TheComeOnMan


1 Answers

A simple solution:

mrg<-(merge(df1,df2, by = 'a', suffixes = c("1","2")))
setnames(mrg,paste0(names(mrg),ifelse(names(mrg) %in% setdiff(names(df1),names(df2)),"1","")))
setnames(mrg,paste0(names(mrg),ifelse(names(mrg) %in% setdiff(names(df2),names(df1)),"2","")))

> names(mrg)
[1] "a"  "b1" "d1" "d2"

Edit: thanks to comments by Ricardo Saporta for cleaning this up considerably and teaching me a few new tips!

like image 130
mrip Avatar answered Oct 28 '22 15:10

mrip