使用一个data.frame更新另一个 [英] Using one data.frame to update another

查看:118
本文介绍了使用一个data.frame更新另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定两个与列名称/数据类型相同的数据帧,其中一些列唯一标识行,是否有一个有效的函数/方法用于一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆