是否可以在合并中使用列索引? [英] Is it possible to use column indices in merge?
本文介绍了是否可以在合并中使用列索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有两个要合并的数据框,是否可以通过列索引而不是列名称进行合并?
If I have two dataframes that I wish to merge, is there a way to merge by the column index rather than the name of the column?
例如,如果我有这两个 dfs,并且想要在 x.x1 和 y.x2 上合并.
For instance if I have these two dfs, and want to merge on x.x1 and y.x2.
dtest <- data.frame(x1 = 1:10, y = 2:11)
dtest2 <- data.frame(x2 = 1:10, y1 = 11:20)
我尝试了以下方法,但无法正常工作
I've tried the following but I can't get it to work
xy <- merge(dtest, dtest2, by.x = x[,1], by.y = y[,1], all.x = TRUE, all.y = TRUE)
推荐答案
给你:
xy <- merge(dtest, dtest2, by.x = 1, by.y = 1, all.x = TRUE, all.y = TRUE)
来自 help(merge)
:可以通过名称、数字或逻辑向量指定要合并的列...
From help(merge)
: Columns to merge on can be specified by name, number or by a logical vector...
这篇关于是否可以在合并中使用列索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文