suffixes
inmerge
仅适用于常见的列名。无论如何,在合并之前无需手动更新列的情况下,是否也可以将其扩展到其他列?
那是 -
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
我目前处理这个问题的方式类似于@mrip 的回答。
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"