合并到一组数据并替换当前值,如果在更新列中
使用以下数据帧。我想创建一个新数据
df1 <- data.frame(ID = LETTERS[1:10],Quantity = c(1,2,3,4,5,6,7,8,9,10))
df2 <- data.frame(Cur_ID = c("A","C","D","H"), Update_ID = c("A","C","X","Y"), Update_Quantity = c(20, 21, 22, 23))
desired df3:
ID letters
A 20
B 2
C 21
X 22
E 5
F 6
G 7
Y 23
I 9
J 10
框DF2中的所有内容均为更新,如果update_id与cur_id不同。
With the following data frames. I would like to create a new data frame where quantities for IDs in df2 are updated with the 'Update quantities' values and IDs are replaces with the 'Update_ID' values (which may or may not change from the current ID.
df1 <- data.frame(ID = LETTERS[1:10],Quantity = c(1,2,3,4,5,6,7,8,9,10))
df2 <- data.frame(Cur_ID = c("A","C","D","H"), Update_ID = c("A","C","X","Y"), Update_Quantity = c(20, 21, 22, 23))
desired df3:
ID letters
A 20
B 2
C 21
X 22
E 5
F 6
G 7
Y 23
I 9
J 10
i.e. the quantities for all in df2 are updates, and the IDs if Update_ID is different from Cur_ID.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
dplyr
,您可以合并df2
中df1
,并使用coalesce()
更新id
id 和数量
。带有
PowerJoin
的另一个选项:输出
With
dplyr
, you could mergedf2
intodf1
and usecoalesce()
to updateID
andQuantity
.Another option with
powerjoin
:Output
使用
data.table
JOIN-OUTPUT
Using
data.table
join-output
采用基本方法:
With a base R approach: