【发布时间】:2021-03-16 20:11:00
【问题描述】:
这似乎是一个有点基本的问题,但在查看文档并搜索互联网后,我找不到答案。
我的问题是:R 的dplyr 是否包含一个参数以在加入两个数据框后自动生成具有合并结果的列?对于连接操作,我的意思是 left_join、right_join 或 full_join。
我正在考虑类似于_merge 列的东西,您可以在使用merge 时使用参数indicator = True 在Python 的pandas 上生成,或者使用默认生成的Stata 的_merge 变量使用merge 命令。
例如:
df1 <- data.frame(id = c(1,2), value1 = c(1,1))
df2 <- data.frame(id = c(1,3), value2 = c(2,1))
result <- df1 %>% full_join(df2, by = 'id', merge_results_option = TRUE)
# Notice that merge_results_option is the argument I'm looking for with this question, if it exists
应该让步:
|--------------|---------------|--------------|--------------|
| id | value1 | value2 | _merge |
|--------------|---------------|--------------|--------------|
| 1 | 1 | 2 | both |
| 2 | 1 | NA | left |
| 3 | NA | 1 | right |
|--------------|---------------|--------------|--------------|
【问题讨论】:
-
如果你给出一个简短的可重复的例子,这个问题会更清楚。您能否展示 2 个示例输入数据帧,每帧 3 行以及您想要的结果?
-
谢谢,现在更新了