通过合并多个列来更新列 [英] Update columns by joining more than one columns
本文介绍了通过合并多个列来更新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表,我需要从第二个表中的 pro_sales
值更新第一个表中的pro_sales列值。
I have two tables and I need to update pro_sales column values in the first table from pro_sales
values in the second.
df1 <- data.frame(storecode = c(100,100,100,200,200),
productcode = c(1,2,3,1,2), pro_sales = c(0,0,0,0,0))
df2 <- data.frame(storecode = c(100,100,200),
productcode = c(1,2,1), pro_sales = c(0,1,0))
我需要在 storecode
和 productcode
列上左联接。下面应该是我的决赛桌:
I need to left join on the columns storecode
and productcode
. Below should be my final table:
storecode productcode pro_sales
1 100 1 0
2 100 2 1
3 100 3 0
4 200 1 0
5 200 2 0
我能够退出dplyr,但是之后我需要帮助吗?
I was able to left join in dplyr but after that i need help please?
df1 %>%
left_join(df2,c("storecode"="storecode","productcode"="productcode")) %>%
mutate( ???? ) %>%
select(names, match, value = value.x)
谢谢。
推荐答案
另一个选择是对 data.table
-package使用更新联接:
Another option is to use an update join with the data.table
-package:
library(data.table)
setDT(df1)
setDT(df2)
df1[df2, on = .(storecode, productcode), pro_sales := i.pro_sales][]
给出
storecode productcode pro_sales
1: 100 1 0
2: 100 2 1
3: 100 3 0
4: 200 1 0
5: 200 2 0
这篇关于通过合并多个列来更新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文