r - 在组合具有相同名称的列表列表的元素时保留嵌套元素名称
问题描述
我有一个列表如下。
dlist <- list(a = list(a1 = list(k = 25, m = 34)),
b = list(b1 = list(k = 23, m = 58)),
c = list(c1 = list(k = NA, m = 32)),
a = list(a2 = list(k = 42, m = 35)),
c = list(c2 = list(k = 41, m = 87)),
d = list(d1 = list(k = 48, m = 90)),
b = list(b2 = list(k = 85, m = 98)),
b = list(b3 = list(k = 47, m = 78)))
str(dlist)
List of 8
$ a:List of 1
..$ a1:List of 2
.. ..$ k: num 25
.. ..$ m: num 34
$ b:List of 1
..$ b1:List of 2
.. ..$ k: num 23
.. ..$ m: num 58
$ c:List of 1
..$ c1:List of 2
.. ..$ k: logi NA
.. ..$ m: num 32
$ a:List of 1
..$ a2:List of 2
.. ..$ k: num 42
.. ..$ m: num 35
$ c:List of 1
..$ c2:List of 2
.. ..$ k: num 41
.. ..$ m: num 87
$ d:List of 1
..$ d1:List of 2
.. ..$ k: num 48
.. ..$ m: num 90
$ b:List of 1
..$ b2:List of 2
.. ..$ k: num 85
.. ..$ m: num 98
$ b:List of 1
..$ b3:List of 2
.. ..$ k: num 47
.. ..$ m: num 78
我想组合具有相同名称的元素。有一个解决方案是结合具有相同名称的列表列表的元素。但是这里没有保留嵌套组件的名称。
dlist2 <- tapply(unlist(dlist, use.names = F, recursive = F),
names(dlist), c)
str(dlist2)
List of 4
$ a:List of 2
..$ :List of 2
.. ..$ k: num 25
.. ..$ m: num 34
..$ :List of 2
.. ..$ k: num 42
.. ..$ m: num 35
$ b:List of 3
..$ :List of 2
.. ..$ k: num 23
.. ..$ m: num 58
..$ :List of 2
.. ..$ k: num 85
.. ..$ m: num 98
..$ :List of 2
.. ..$ k: num 47
.. ..$ m: num 78
$ c:List of 2
..$ :List of 2
.. ..$ k: logi NA
.. ..$ m: num 32
..$ :List of 2
.. ..$ k: num 41
.. ..$ m: num 87
$ d:List of 1
..$ :List of 2
.. ..$ k: num 48
.. ..$ m: num 90
- attr(*, "dim")= int 4
- attr(*, "dimnames")=List of 1
..$ : chr [1:4] "a" "b" "c" "d"
我正在使用以下代码来保留列表的嵌套组件的名称。
dlist3 <- tapply(unlist(dlist, use.names = T, recursive = F),
names(dlist), c)
dlist3 <- lapply(dlist3,
function(x) {names(x) <- gsub("^(.+)(\\.)",
"", names(x)); return(x)})
str(dlist3)
List of 4
$ a:List of 2
..$ a1:List of 2
.. ..$ k: num 25
.. ..$ m: num 34
..$ a2:List of 2
.. ..$ k: num 42
.. ..$ m: num 35
$ b:List of 3
..$ b1:List of 2
.. ..$ k: num 23
.. ..$ m: num 58
..$ b2:List of 2
.. ..$ k: num 85
.. ..$ m: num 98
..$ b3:List of 2
.. ..$ k: num 47
.. ..$ m: num 78
$ c:List of 2
..$ c1:List of 2
.. ..$ k: logi NA
.. ..$ m: num 32
..$ c2:List of 2
.. ..$ k: num 41
.. ..$ m: num 87
$ d:List of 1
..$ d1:List of 2
.. ..$ k: num 48
.. ..$ m: num 90
有没有更优雅的方法来做到这一点?
解决方案
使用Map
,Reduce
和append
函数,您可以按如下方式解决问题:
dlist3 <- Map(function(x) Reduce(append, dlist[names(dlist)==x]), unique(names(dlist)))
str(dlist3)
# List of 4
# $ a:List of 2
# ..$ a1:List of 2
# .. ..$ k: num 25
# .. ..$ m: num 34
# ..$ a2:List of 2
# .. ..$ k: num 42
# .. ..$ m: num 35
# $ b:List of 3
# ..$ b1:List of 2
# .. ..$ k: num 23
# .. ..$ m: num 58
# ..$ b2:List of 2
# .. ..$ k: num 85
# .. ..$ m: num 98
# ..$ b3:List of 2
# .. ..$ k: num 47
# .. ..$ m: num 78
# $ c:List of 2
# ..$ c1:List of 2
# .. ..$ k: logi NA
# .. ..$ m: num 32
# ..$ c2:List of 2
# .. ..$ k: num 41
# .. ..$ m: num 87
# $ d:List of 1
# ..$ d1:List of 2
# .. ..$ k: num 48
# .. ..$ m: num 90
推荐阅读
- github - 是否可以使用自定义参数获取 github 令牌页面?
- asp.net-mvc - 在没有数据库的情况下存储数据
- python - ValueError:未能找到可以处理输入的数据适配器:
, - java - 有没有办法在 Java 8 运行时中使用 flatc 生成的类?
- pdf - 嵌入 PDF 更改 Vivaldi 中以下网页的缩放值
- python - 如何连接熊猫数据框中的连续行?
- c# - PayPal API v2 取消订单
- python - 将 django 制作的网页上传到 aws 时出现问题
- django - django PostgreSQL arrayfield 存储一个 3 维数组
- java - 将 struts 从 2.5.16 升级到 2.5.26 时出现 404 未找到错误