【发布时间】:2022-01-17 13:54:32
【问题描述】:
我有 2 个大小不等的数据框,我想根据“颜色”列将值从较小的数据框映射到较大的数据框。
df1:
| Color | Value |
|---|---|
| Red | A |
| Blue | B |
| Green | C |
df2:
| Color | Repetition |
|---|---|
| Red | 1 |
| Green | 1 |
| Red | 2 |
| Blue | 1 |
| Blue | 2 |
| ... | ... |
期望的输出:
| Color | Repetition | Value |
|---|---|---|
| Red | 1 | A |
| Green | 1 | C |
| Red | 2 | A |
| Blue | 1 | B |
| Blue | 2 | B |
| ... | ... | ... |
可重现的代码:
df1 <- data.frame(c("Red", "Blue", "Green"),
c("A", "B", "C"))
names(df1) <- c("Color", "Value")
df2 <- data.frame(c("Red", "Green", "Red", "Blue", "Blue"),
c(1,1,2,1,2))
names(df2) <- c("Color", "Repetition")
我尝试过合并,但它对我不起作用,可能是因为它们的大小不同。我也试过用谷歌搜索这个问题,但只找到了 python 解决方案。
【问题讨论】: