Mike
Mike

Reputation: 11

Accessing multi-dimensional array using a one-dimensional index in R

I have a 3-dimensional array with dim = c(50,100,12). Now I want to access the grid point that corresponds to the one-dimensional index 123. I want to obtain a vector of 12 values from grid point 123. How do I achieve that? Thanks a lot!

Upvotes: 1

Views: 529

Answers (2)

Mike
Mike

Reputation: 11

Well, I finally solved the problem by converting the vector indices into array indices using the R base package implemented function arrayInd(ind, dim). It returns a two-dimensional matrix with the corresponding array indices that looks like that:

   [,1] [,2]
 [1,]  207  129
 [2,]  197  138
 [3,]  199  136
 [4,]  205  131

Upvotes: 0

IRTFM
IRTFM

Reputation: 263332

With R, you have a choice of indexing with arrays and matrices. You can use the dimensional indexing or you can use vector indexng. Just use:

myArray[123:(123+11) ]

Upvotes: 2

Related Questions