介绍
我有两个嵌套列表,它们的结构要合并(在c()
意义上)。
对于这种关系,在图论或计算机科学中可能已经存在一个我用相同结构表示的概念,但我不知道。
因此,这是我尝试阐明相同结构的意思:
某个级别的列表元素要么全部命名,要么没有命名。
当我们为元素命名时,在该级别上永远不会有重复的名称。
当节点本身被命名为元素时,两个列表的父子节点关系相同。
所以我想知道是否已经有解决这个问题的方法,我认为这可能是相当普遍和普遍的...(?)任何涉及以下内容的解决方案:
使用基本rapply
;
Tidyverse解决方案与purrr
函数的某种组合;rlist
包中的功能
会很好!
例foo
和bar
是两个具有相同结构的示例列表。wonderful
是将foo
和bar
(手动完成)组合而成的所需列表。
我希望它足够清楚!
# Input lists: foo and bar
foo <- list(a = list(a1 = 1:3, a2 = rep('a', 3)), b = list(b1 = list(b11 = c(4,5,6), b12 = rep('b', 3)), b2 = list(b21 = list(b31 = c(0, 1, 2)))), c = list(list(c21 = 1:3), list(c21 = 4:6), list(c21 = 7:9)))
bar <- list(a = list(a1 = 1:3, a2 = rep('z', 3)), b = list(b1 = list(b11 = c(-1,2,5), b12 = rep('b', 3)), b2 = list(b21 = list(b31 = -c(1,2,3)))), c = list(list(c21 = 3:1), list(c21 = 5:3)))
# wonderful: desired list (result from combining foo and bar)
wonderful <- list(
a = list(
a1 = c(foo$a$a1, bar$a$a1),
a2 = c(foo$a$a2, bar$a$a2)
),
b = list(
b1 = list(
b11 = c(foo$b$b1$b11, bar$b$b1$b11),
b12 = c(foo$b$b1$b12, bar$b$b1$b12)
),
b2 = list(
b21 = list(
b31 = c(foo$b$b2$b21$b31, bar$b$b2$b21$b31)
)
)
),
c = c(foo$c, bar$c)
)
str(foo)
#> List of 3
#> $ a:List of 2
#> ..$ a1: int [1:3] 1 2 3
#> ..$ a2: chr [1:3] "a" "a" "a"
#> $ b:List of 2
#> ..$ b1:List of 2
#> .. ..$ b11: num [1:3] 4 5 6
#> .. ..$ b12: chr [1:3] "b" "b" "b"
#> ..$ b2:List of 1
#> .. ..$ b21:List of 1
#> .. .. ..$ b31: num [1:3] 0 1 2
#> $ c:List of 3
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 1 2 3
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 4 5 6
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 7 8 9
str(bar)
#> List of 3
#> $ a:List of 2
#> ..$ a1: int [1:3] 1 2 3
#> ..$ a2: chr [1:3] "z" "z" "z"
#> $ b:List of 2
#> ..$ b1:List of 2
#> .. ..$ b11: num [1:3] -1 2 5
#> .. ..$ b12: chr [1:3] "b" "b" "b"
#> ..$ b2:List of 1
#> .. ..$ b21:List of 1
#> .. .. ..$ b31: num [1:3] -1 -2 -3
#> $ c:List of 2
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 3 2 1
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 5 4 3
str(wonderful)
#> List of 3
#> $ a:List of 2
#> ..$ a1: int [1:6] 1 2 3 1 2 3
#> ..$ a2: chr [1:6] "a" "a" "a" "z" ...
#> $ b:List of 2
#> ..$ b1:List of 2
#> .. ..$ b11: num [1:6] 4 5 6 -1 2 5
#> .. ..$ b12: chr [1:6] "b" "b" "b" "b" ...
#> ..$ b2:List of 1
#> .. ..$ b21:List of 1
#> .. .. ..$ b31: num [1:6] 0 1 2 -1 -2 -3
#> $ c:List of 5
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 1 2 3
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 4 5 6
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 7 8 9
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 3 2 1
#> ..$ :List of 1
#> .. ..$ c21: int [1:3] 5 4 3
最佳答案
这是一个尝试:
library(purrr)
rec_map <- function(fizz, buzz) {
if(is.atomic(fizz) | is.null(names(fizz))){
c(fizz, buzz)
} else {
imap(fizz,
~rec_map(fizz[[.y]], buzz[[.y]]))
}
}
temp <- rec_map(foo, bar)
all.equal(temp, wonderful)
#> [1] TRUE
我绝不是计算机科学家,所以请带上一粒盐。当没有一级名称时,我不确定所需的行为,但是在下一级则有名称(例如
foo$c
)。因此,如果遇到没有名称的关卡,我只是合并结果(c()
)。编辑以获取许多列表:
prec_map <- function(...){
dots <- list(...)
first_el = dots[[1]]
if(is.atomic(first_el) | is.null(names(first_el))){
do.call(c, dots)
} else {
imap(first_el,
function(el, nme){
one_level_down <- map(dots, nme)
do.call(prec_map, one_level_down)
})
}
}
temp <- prec_map(foo, bar)
all.equal(temp, wonderful)
[1] TRUE
我还没有对它进行彻底的测试,但是光测试似乎可以完成工作。