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!
2 Answers
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) ]
4 Comments
IRTFM
So it wasn't an array. Terminology is important in these matters. It appears you intended (or at least should have intended) to say that you were accessing a nested list and that the index was c(3, 2, 1).
IRTFM
We can only guess at what "this" might have been. It would not be surprising if my suggestion failed with a list. It was designed to work with an array. Post a reproducible example if you are still having problems.
Mike
It is a nested list, right. It is one list of 12 lists with each of them containing one array.... That is why I tried climate[[i]][k][d,f,g]. Isn't it possible that way?
IRTFM
Not likely that way. Possibly
climate[[i]][[k]][c(d,f,g)] but an example would be needed to be sure. Or you can post the results of str(.) on the object of interest.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
as.vector(ThreeD)[123:134]?