基于公共列合并数据帧,但保留x中的所有行 [英] merge dataframes based on common columns but keeping all rows from x
问题描述
我需要合并两个数据框 x 和 y ,它们共有大约50列公共列和一些唯一列,并且我需要保留中的所有行x 。
I need to merge two dataframes x and y which have about 50 columns in common and some unique columns, and I need to keep all the rows from x.
如果我运行,它会起作用:
It works if I run:
NewDataframe <- merge(x, y, by=c("ColumnA", "ColumnB", "ColumnC"),all.x=TRUE)
问题是共有50多个公共列,我宁愿避免键入所有公共列的名称。
The issue is that there are more than 50 common columns, and I would rather avoid typing the names of all the common columns.
我尝试过:
NewDataframe <- merge(x, y, all.x=TRUE)
但是出现以下错误:
Error in merge.data.table(x, y, all.x = TRUE) :
Elements listed in `by` must be valid column names in x and y
有什么方法可以将 by
与常见列一起使用,而无需键入所有内容,但保留所有 x 中的行?
Is there any way of using by
with the common columns without typing all of them, but keeping all the rows from x?
谢谢。
推荐答案
我认为您拥有数据表,而不是简单的数据框,并且两者之间的合并工作略有不同。您可以尝试通过使用 NewDataframe<-merge.data.frame(x,y,all.x = TRUE)
强制其使用dataframe方法在所有共享列名上。
I think you have data tables rather than simple dataframes, and merge works slightly differently between the two. You could try forcing it to use the dataframe method by using NewDataframe <- merge.data.frame(x, y, all.x=TRUE)
which should by default merge on all shared column names.
这篇关于基于公共列合并数据帧,但保留x中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!