How to index a multidimensional R array dynamically?

前端 未结 3 371
我寻月下人不归
我寻月下人不归 2021-01-20 12:52

I am working on some data in R that consist of four-dimensional arrays composed of three spatial dimensions and a time dimension: x, y, z, t. For some of my analyses, I woul

3条回答
  •  有刺的猬
    2021-01-20 13:19

    Accessing by each dimension individually does not work as @tilo-wiklund or I expect. Instead of 23 rows across 10 time steps, the result is a 23x23x23 cube across the 10 time steps.

    r.idvdim <- a4d[indices[,1],indices[,2],indices[,3],]
    r.apply  <- apply(a4d, 4, `[`, indices)
    r.cbind  <- matrix(a4d[lookup],ncol=nt)
    
    dim(r.idvdim)     # [1] 23 23 23 10
    dim(r.apply)      # [1] 23 10
    dim(r.cbind)      # [1] 23 10
    

提交回复
热议问题