R-使用覆盖和递归合并列表 [英] R - merge lists with overwrite and recursion

查看:87
本文介绍了R-使用覆盖和递归合并列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个带有名称的列表,

Suppose I have two lists with names,

a = list( a=1, b=2, c=list( d=1, e=2 ), d=list( a=1, b=2 ) )
b = list( a=2, c=list( e=1, f=2 ), d=3, e=2 )

我想递归合并这些列表,如果第二个参数包含冲突值,则覆盖条目. IE.预期的输出将是

I'd like to recursively merge those lists, overwriting entries if the second argument contains conflicting values. I.e. the expected output would be

$a
[1] 2

$b
[1] 2

$c
$c$d
[1] 1

$c$e
[1] 1

$c$f
[1] 2

$d
[1] 3

$e
[1] 2

有任何提示吗?

推荐答案

我不确定此处是否需要自定义函数.有一个功能utils::modifyList()可以执行完全相同的操作!有关更多信息,请参见 modifyList . /p>

I am not so sure if a custom function is necessary here. There is a function utils::modifyList() to perform this exact same operation! See modifyList for more info.

a <- list( a=1, b=2, c=list( d=1, e=2 ), d=list( a=1, b=2 ) )
b <- list( a=2, c=list( e=1, f=2 ), d=3, e=2 )

modifyList(a, b) # updates(modifies) 'a' with 'b'

给出以下内容

$a
[1] 2

$b
[1] 2

$c
$c$d
[1] 1

$c$e
[1] 1

$c$f
[1] 2

$d
[1] 3

$e
[1] 2 

这篇关于R-使用覆盖和递归合并列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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