合并数据框并覆盖值 [英] Merge data frames and overwrite values
问题描述
如何合并 2 个相似的数据框,但其中一个更重要?
How do I merge 2 similar data frames but have one with greater importance?
例如:
数据框 1
Date Col1 Col2
jan 2 1
feb 4 2
march 6 3
april 8 NA
数据框 2
Date Col2 Col3
jan 9 10
feb 8 20
march 7 30
april 6 40
按日期合并这些数据框 1 优先但数据框 2 填充空白
merge these by Date with dataframe 1 taking precedence but dataframe 2 filling blanks
数据框合并
Date Col1 Col2 Col3
jan 2 1 10
feb 4 2 20
march 6 3 30
april 8 6 40
编辑 - 解决方案
commonNames <- names(df1)[which(colnames(df1) %in% colnames(df2))]
commonNames <- commonNames[commonNames != "key"]
dfmerge<- merge(df1,df2,by="key",all=T)
for(i in commonNames){
left <- paste(i, ".x", sep="")
right <- paste(i, ".y", sep="")
dfmerge[is.na(dfmerge[left]),left] <- dfmerge[is.na(dfmerge[left]),right]
dfmerge[right]<- NULL
colnames(dfmerge)[colnames(dfmerge) == left] <- i
}
推荐答案
merdat <- merge(dfrm1,dfrm2, by="Date") # seems self-documenting
# explanation for next line in text below.
merdat$Col2.y[ is.na(merdat$Col2.y) ] <- merdat$Col2.x[ is.na(merdat$Col2.y) ]
然后将'merdat$Col2.y'重命名为'merdat$Col2'并删除'merdat$Col2.x'.
Then just rename 'merdat$Col2.y' to 'merdat$Col2' and drop 'merdat$Col2.x'.
回复更多评论请求:仅更新向量部分的一种方法是构造一个用于索引的逻辑向量,并使用["将其应用于赋值的两侧.另一种方法是设计一个仅在赋值的 LHS 上的逻辑向量,然后使用 rep()
创建一个与 sum(logical.vector)
具有相同长度的向量代码>.目标是两个实例的分配长度(和顺序)与被替换的项目相同.
In reply to request for more comments: One way to update only sections of a vector is to construct a logical vector for indexing and apply it using "[" to both sides of an assignment. Another way is to devise a logical vector that is only on the LHS of an assignment but then make a vector using rep()
that has the same length as sum(logical.vector)
. The goal is both instances is to have the same length (and order) for assignment as the items being replaced.
这篇关于合并数据框并覆盖值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!