简体   繁体   English

R 覆盖另一个列表中的列表值

[英]R Overwrite list values from another list

I need to merge two lists so that the values of the second list overwrites the corresponding list item in the first if there are duplicates.我需要合并两个列表,以便第二个列表的值覆盖第一个列表中的相应列表项(如果有重复项)。 Is there a way to do this without using a slow for loop in R?有没有办法在不使用 R 中的慢循环的情况下做到这一点?

A simple example:一个简单的例子:

A <- list("First"=1,"Second"=2)
B <- list("First"=3,"Third"=3)
C <- A
for(curr in names(B)){ C[curr] <- B[curr] }

and the content of C is now现在 C 的内容是

> C
$First
[1] 3

$Second
[1] 2

$Third
[1] 3

what is what I want.我想要什么。 But, could this be done without the for loop?但是,这可以在没有 for 循环的情况下完成吗?

You can use names of B to change value in C .您可以使用B names来更改C值。

C[names(B)] <- B
C

#$First
#[1] 3

#$Second
#[1] 2

#$Third
#[1] 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM