Consider the following code example:
The objective is to replace the indices in mymatrix with the corresponding values in mypoints.
# example 1---------------------------------------------
mymatrix <- c(3,4,1,2, 2,1,4,3, 4,3,2,1)
dim(mymatrix) <- c(4,3)
mypoints <- matrix(c(10, 20, 30, 40), nrow(mymatrix), 1)
# loop
mysummary <- mypoints[mymatrix[,1]]
for (i in 2:ncol(mymatrix) ) {
mysummary <- cbind(mysummary, mypoints[mymatrix[,i]])
}
# ------------------------------------------------------
The result is:
mysummary
[1,] 30 20 40
[2,] 40 10 30
[3,] 10 40 20
[4,] 20 30 10
The loop can be replaced in a more concise way:
> mysummary <- mypoints[mymatrix]
> dim(mysummary) <- dim(mymatrix)
The result:
[,1] [,2] [,3]
[1,] 30 20 40
[2,] 40 10 30
[3,] 10 40 20
[4,] 20 30 10
However, this does not work in following smaller example:
# example 3---------------------------------------------
> mymatrix <- c(2,1, 2,1)
> dim(mymatrix) <- c(2,2)
> mypoints <- matrix(c(10, 20), nrow(mymatrix), 1)
> mysummary <- mypoints[mymatrix]
Error in mypoints[mymatrix] : subscript out of bounds
I have no idea which subscript is out of bounds and in which array. And how to prevent this error.
If the indexing matrix mymatrix
has two columns, then R assumes that you want to use the first column as the row index and the second as the column index. So it fails as the first row of mymatrix
in your second example is 2,2
, which is out of bounds in mypoints
.
In your first example, mymatrix
has more than 2 columns, so R treats it as a vector, which is the only way that makes sense in the circumstances.