我有两个数据框,我希望组合唯一值并单独更新现有记录的列值,而不是添加新记录。
DF1<-data.frame(name = c("Test1","Test2","Test3"),
Value = c(10,20,30),
Remarks = c("Good","Best","Perfect")
)
DF2<- data.frame(name = c("Test","Test2","Test3","Test4"),
Value = c(10,22,30, 40),
Remarks = c("Good","Best","Perfect","NEW"))
我已经尝试过
DF3 <- unique(bind_rows(x = DF1, y = DF2))
结果是
name Value Remarks
1 Test1 10 Good
2 Test2 20 Best
3 Test3 30 Perfect
4 Test 10 Good
5 Test2 22 Best
7 Test4 40 NEW
但我渴望实现
name Value Remarks
1 Test1 10 Good
2 Test2 **22** Best
3 Test3 30 Perfect
4 Test 10 Good
5 Test4 40 NEW
对于现有值,我希望更新而不是添加新记录。
最佳答案
我们可以使用rows_upsert
library(dplyr)
rows_upsert(DF1, DF2)
-输出
name Value Remarks
1 Test1 10 Good
2 Test2 22 Best
3 Test3 30 Perfect
4 Test 10 Good
5 Test4 40 NEW
关于r - 根据条件使用另一个值的值更新数据框中的整个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74959113/