R - how to get a value of a multi-dimensional array by a vector of indices

前端 未结 3 1623
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 10:32

Let\'s say I have a multi-dimensional array called pi, and its number of dimensions isn\'t known until the runtime:

dims <- rep(3, dim_count)         


        
3条回答
  •  执笔经年
    2020-12-06 11:11

    do.call("[",...) seems to work.

    indexes <- c(1,2,3,3,3)
    pi[1,2,3,3,3] <- 17  ## so we know if we succeeded or not
    do.call("[",c(list(pi),as.list(indexes)))
    

    Note that your example wouldn't work -- your dimensions were all 3, but some of your index elements were >3 ...

提交回复
热议问题