Extracting values from multidimensional array

Viewed 613

I have array arr that can have any number of dimensions, say

arr <- array(NA, c(2,3,5))

I need a function getArrValues that takes as input a vector and uses this vector to extract values from the array:

getArrValues(arr, c(1,2,2)) # = arr[1,2,2]
getArrValues(arr, c(1,2))   # = arr[1,2,]
getArrValues(arr, c(1))     # = arr[1,,]

Do you have any ideas how this could be achieved? There is always a single index for a dimension, indexes are provided as a vector in the order of array dimensions, arrays can have any number of dimensions that is always greater or equal from the length of input vector.

1 Answers
Related