【问题标题】:Stacking array slices (rbind for >2 dimensions)堆叠数组切片(>2 维的 rbind)
【发布时间】:2013-02-13 15:47:15
【问题描述】:

我有一系列 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
   }
} 

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

【问题讨论】:

    标签: r


    【解决方案1】:

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

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

    除了Curry 解决方案之外,这似乎也有效:

    do.call("abind",list(la,along = 1))
    

    【讨论】:

    • 可以,谢谢!我使用do.call( Curry(abind, along=1), la ) 来避免输入每一个。
    猜你喜欢
    • 1970-01-01
    • 2018-09-26
    • 2019-05-14
    • 2015-01-29
    • 2020-05-03
    • 2021-11-23
    • 2014-08-25
    • 1970-01-01
    相关资源
    最近更新 更多