覆盖现有数据帧的值 [英] Overwrite values of existing dataframe
问题描述
如果有一个数据框可以保存2种类型的观察值,请按ID编号( id.1
, id.2
val.1 , val.2
)以及本示例中表示的其他几个数据 val.other
。
If have a data frame which holds 2 type of observations, coded by IDs (id.1
, id.2
) with corresponding values (val.1
, val.2
) and several other data represented in this example by val.other
.
set.seed(1)
# df.master
id.1= c("abc", "def", "ghi", "jkl")
val.1= c(1, 2, 3, 4)
id.2= c("mno", "pqr", "stu", "vwx")
val.2= c(5, 6, 7, 8)
val.other= rep(runif(1),4)
df.master= data.frame(id.1, id.2, val.other, val.1, val.2)
df.master
看起来像:
id.1 id.2 val.other val.1 val.2
1 abc mno 0.2655087 1 5
2 def pqr 0.2655087 2 6
3 ghi stu 0.2655087 3 7
4 jkl vwx 0.2655087 4 8
我生成第二和第三个数据帧中单独存储的新数据 df.new.1
和 df.new.2
。
I generate new data stored separately in a 2nd and 3rd data frame df.new.1
and df.new.2
.
df.new.1
看起来像:
id.3 val.3
1 abc 10
2 ghi 20
3 stu 30
# Create an 2nd data frame, which contains new values
id.3= c("abc", "ghi", "stu")
val.3= c(10, 20, 30)
df.new.1= data.frame(id.3, val.3)
df.new.2
看起来像:
id.4 val.4
1 def 100
2 vwx 200
# Create an 3rd data frame, which contains new values
id.4= c("def", "vwx")
val.4= c(100, 200)
df.new.2= data.frame(id.4, val.4)
我想根据 df.new.1的内容更新
df.master
code>和 df.new.2
同时保持原始结构 df.master
导致以下结果:
I want to update df.master
based on contents of df.new.1
and df.new.2
while keeping the original structure of df.master
leading to following result:
id.1 id.2 val.other val.1 val.2
1 abc mno 0.2655087 10 5
2 def pqr 0.2655087 100 6
3 ghi stu 0.2655087 20 30
4 jkl vwx 0.2655087 4 200
请注意, df.new.1
和 df.new.2
包含新的数据匹配 id.1
和 id.2
df.master
。
Please note that df.new.1
and df.new.2
contain a mix of new data matching id.1
and id.2
of df.master
.
任何建议代码执行更新的 df.master
?
Any suggestions for code to perform the update of df.master
?
推荐答案
以下内容可能会有所帮助:
Something like the following could be helpful:
ids_mat = as.matrix(df.master[c("id.1", "id.2")])
mat_inds = arrayInd(match(df.new.1$id.3, ids_mat), dim(ids_mat))
df.master[c("val.1", "val.2")][mat_inds] <- df.new.1$val.3
df.master
# id.1 id.2 val.other val.1 val.2
#1 abc mno 0.2655087 10 5
#2 def pqr 0.2655087 2 6
#3 ghi stu 0.2655087 20 30
#4 jkl vwx 0.2655087 4 8
与 df.new.2
相同的逻辑
这篇关于覆盖现有数据帧的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!