保留嵌套元素名称

保留嵌套元素名称

本文介绍了保留嵌套元素名称,同时合并具有相同名称的列表列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表,如下所示.

I have a list of lists as follows.

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

我想合并具有相同名称的元素.具有相同名称的列表列表的组合元素中有一个解决方案.但是这里没有保留嵌套组件的名称.

I want to combine elements with same name. There is a solution in combine elements of list of lists with the same name. But here the names of the nested components are not preserved.

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"

我正在使用以下代码来保留列表的嵌套组件的名称.

I am using the following code to preserve the names of the nested components of the list.

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

是否有更优雅的方法来做到这一点?

Is there a more elegant way to do this ?

推荐答案

使用 Map Reduce append 函数,您可以解决问题如下:

Using Map, Reduce and append functions, You could solve the problem as follows:

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

这篇关于保留嵌套元素名称,同时合并具有相同名称的列表列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:12