使用一个data.frame更新另一个 [英] Using one data.frame to update another
问题描述
给定两个与列名称/数据类型相同的数据帧,其中一些列唯一标识行,是否有一个有效的函数/方法用于一个data.frame更新另一个?
例如,在以下内容中,原始
和替换
code>'Name'和'Id'
。 目标
是在原始
替换中的所有行的结果>(通过唯一ID)并替换为 Value1
和 Value2
= code>原始= data.frame(Name = c(joe,john),Id = c(1,2),Value1 = c(1.2,NA) Value2 = c(NA,9.2))
replacement = data.frame(Name = c(john),Id = 2,Value1 = 2.2,value2 = 5.9)
goal = data.frame Name = c(joe,john),Id = c(1,2),Value1 = c(1.2,2.2),Value2 = c(NA,5.9))
解决方案应该适用于原始
和替换
任意长度(尽管替换
不应该有比原始
更多的行)。实际上,我使用2个id列。
只需设置唯一的ID作为行名称。然后它是简单的索引:
rownames(original)= original $ id
rownames(replacement)= replacement $ id
original [rownames(replacement),] = replacement
Given 2 data frames that are identical in terms of column names/datatypes, where some columns uniquely identify the rows, is there an efficient function/method for one data.frame to "update" the other?
For example, in the following, original
and replacement
are identified by 'Name'
and 'Id'
. goal
is the result of finding all rows from replacement
in original
(by the unique id's) and replacing with Value1
and Value2
original = data.frame( Name = c("joe","john") , Id = c( 1 , 2) , Value1 = c(1.2,NA), Value2 = c(NA,9.2) )
replacement = data.frame( Name = c("john") , Id = 2 , Value1 = 2.2 , value2 = 5.9)
goal = data.frame( Name = c("joe","john") , Id = c( 1 , 2) , Value1 = c(1.2,2.2), Value2 = c(NA,5.9) )
The solution should work for an original
and replacement
of arbitrary length (although replacement
should never have more rows than original
). In practice, I'm using 2 id columns.
Just set a unique ID as the row names. Then it is simple indexing:
rownames(original) = original$Id
rownames(replacement) = replacement$Id
original[rownames(replacement), ] = replacement
这篇关于使用一个data.frame更新另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!