R 测试相同结构的嵌套列表

R 测试相同结构的嵌套列表,r,R,假设我有一些复杂的嵌套列表: x <- list(list(a = array(1:24, c(2,3,4)), b = array(1:6, c(3,1,2))), list(c = 'string1'), list(d = c(T, F, F), e = c(F, F, T))) y <- list(list(a = array(24:1, c(2,3,4)), b = array(2:7, c(3,1,2))), list(c = 'string2'), list(d = c(

假设我有一些复杂的嵌套列表:

x <- list(list(a = array(1:24, c(2,3,4)), b = array(1:6, c(3,1,2))), list(c = 'string1'), list(d = c(T, F, F), e = c(F, F, T)))
y <- list(list(a = array(24:1, c(2,3,4)), b = array(2:7, c(3,1,2))), list(c = 'string2'), list(d = c(F, F, F), e = c(T, T, T)))
z <- list(list(a = array(24:1, c(3,2,4)), b = array(2:7, c(3,1,2))), list(c = 'string2'), list(d = c(F, F, F), e = c(T, T, T)))

可以操纵
str
函数来仅比较结构。默认情况下,它输出到终端并返回
NULL
,但我们可以使用
capture.output
获取其输出值:

> x.str <- capture.output(str(x))
> y.str <- capture.output(str(y))
> x.str
[1] "List of 3"                                            
[2] " $ :List of 2"                                        
[3] "  ..$ a: int [1:2, 1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ..."
[4] "  ..$ b: int [1:3, 1, 1:2] 1 2 3 4 5 6"               
[5] " $ :List of 1"                                        
[6] "  ..$ c: chr \"string1\""                             
[7] " $ :List of 2"                                        
[8] "  ..$ d: logi [1:3] TRUE FALSE FALSE"                 
[9] "  ..$ e: logi [1:3] FALSE FALSE TRUE"      
> y.str
[1] "List of 3"                                                     
[2] " $ :List of 2"                                                 
[3] "  ..$ a: int [1:2, 1:3, 1:4] 24 23 22 21 20 19 18 17 16 15 ..."
[4] "  ..$ b: int [1:3, 1, 1:2] 2 3 4 5 6 7"                        
[5] " $ :List of 1"                                                 
[6] "  ..$ c: chr \"string2\""                                      
[7] " $ :List of 2"                                                 
[8] "  ..$ d: logi [1:3] FALSE FALSE FALSE"                         
[9] "  ..$ e: logi [1:3] TRUE TRUE TRUE"    
综合所有这些,我们可以实现以下功能:

all.equal.structure <- function(x, y) {
    identical(capture.output(str(x, vec.len = 0)),
              capture.output(str(y, vec.len = 0)))
}

> all.equal.structure(x, y)
[1] TRUE
> all.equal.structure(y, z)
[1] FALSE
all.equal.structure all.equal.structure(x,y)
[1] 真的
>全等结构(y,z)
[1] 假的
> x.str <- capture.output(str(x, vec.len = 0))
> x.str
[1] "List of 3"                            
[2] " $ :List of 2"                        
[3] "  ..$ a: int [1:2, 1:3, 1:4] NULL ..."
[4] "  ..$ b: int [1:3, 1, 1:2] NULL ..."  
[5] " $ :List of 1"                        
[6] "  ..$ c: chr  ..."                    
[7] " $ :List of 2"                        
[8] "  ..$ d: logi [1:3] NULL ..."         
[9] "  ..$ e: logi [1:3] NULL ..."   
all.equal.structure <- function(x, y) {
    identical(capture.output(str(x, vec.len = 0)),
              capture.output(str(y, vec.len = 0)))
}

> all.equal.structure(x, y)
[1] TRUE
> all.equal.structure(y, z)
[1] FALSE