重铸任何深度的嵌套列表 [英] Recasting nested list of any depth

查看:43
本文介绍了重铸任何深度的嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设此简化示例:

L <- list()

L$Foo <- list()
L$Foo$Bar <- list()
L$Foo$Bar$VAR <- TRUE

L$Lorem <- list()
L$Lorem$Ipsum <- list()
L$Lorem$Ipsum$Dolor <- list()
L$Lorem$Ipsum$Dolor$VAR <- TRUE

然后我将使用 reshape2 :: melt(L)融化此列表.这将输出以下内容:

I will then melt this list with reshape2::melt(L). That will output the following:

  value    L3    L2   L4    L1
1  TRUE   VAR   Bar <NA>   Foo
2  TRUE Dolor Ipsum  VAR Lorem

在对value列中的某些单元格进行一些操作之后,我正在寻找将该熔化的列表重铸为与 L 完全相同的嵌套列表结构–唯一的区别是我更新了一些 value 实例的数量.

After some operations on certain cells in the value column, I'm then looking to recast this melted list into the exact same nested list structure as L—the only difference being that I updated a few of the value instances.

任何想法如何实现这一目标?请记住,嵌套列表的深度可以任意变化.

Any ideas how to achieve this? Please keep in mind that the nested lists can have any, and varying, depth.

推荐答案

在我们取消列出 L L 后,选项是 relist >

An option is relist, after we unlisted L

tmp <- unlist(L)
# make small changes
tmp[] <- FALSE
relist(tmp, L)

结果

$Foo
$Foo$Bar
$Foo$Bar$VAR
[1] FALSE



$Lorem
$Lorem$Ipsum
$Lorem$Ipsum$Dolor
$Lorem$Ipsum$Dolor$VAR
[1] FALSE


L 看起来

$Foo
$Foo$Bar
$Foo$Bar$VAR
[1] TRUE



$Lorem
$Lorem$Ipsum
$Lorem$Ipsum$Dolor
$Lorem$Ipsum$Dolor$VAR
[1] TRUE

这篇关于重铸任何深度的嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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