我有一系列的 lapply 调用给我留下了一个数组列表。第一个维度数量不同,其中两个维度相同。我想将每个“行”(第一维)放在彼此的顶部,就像 rbind 对二维数组(矩阵)所做的那样。

数据如下所示:

la <- list( array( dim=c(1,6,7) ), array( dim=c(2,6,7) ), array( dim=c(3,6,7) ) )

我不能使用 do.call(rbind, la) 因为它只是将它展平为矩阵。

我可以这样写:
rbind.array <- function(..., deparse.level=1) {
   allargs <- list(...)
   n <- length(allargs)
   dims <- sapply(la,dim)
   sel.dim <- which(!apply( dims, 1, function(x) length(unique(x))==1 ))
   stopifnot(length(sel.dim)==1)
   target.length <- apply( dims, 1, sum)[sel.dim]
   target.dims <- dims[,1]
   target.dims[sel.dim] <- target.length
   res <- array( dim=target.dims )
   for( i in seq(n) ) {
      # stack array slices one by one
   }
}

但是这样的功能肯定已经存在了吗?

最佳答案

我认为来自 abind abind 这样做......?

library(abind)
abind(la[[1]],la[[2]],la[[3]],along = 1)

除了 Curry 解决方案,这似乎也有效:
do.call("abind",list(la,along = 1))

关于r - 堆叠数组切片(> 2 维的 rbind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14857358/

10-12 20:48