user3472591
user3472591

Reputation: 41

Changing indexing of a matrix in R

Can someone tell me, if there is a way of changing the indexing of a matrix. So if i am running a loop, the new index is applied. In my example i would have to change the indexing, so that the index of the matrix is not 1:5 but 64:68. Is this possible.

Thanks in advance

a <- matrix(1:20, ncol=4)

a
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20

In this case, the matrix index should start with number 64.

My intended result would be:

     [,1] [,2] [,3] [,4]
[64,]    1    6   11   16
[65,]    2    7   12   17
[66,]    3    8   13   18
[67,]    4    9   14   19
[68,]    5   10   15   20

So if I apply

a[64,]

my result would be

[64]  1  6 11 16

Upvotes: 0

Views: 100

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226911

If you really need this (although I agree that it would be better to describe the context in more detail to see if there is an alternative way to do what you want), you could look at the Oarray package:

library(Oarray)

Note you have to use dim rather than ncol, nrow:

(a <- Oarray(1:20, dim=c(5,4),offset=c(64,1)))
##       [,1] [,2] [,3] [,4]
## [64,]    1    6   11   16
## [65,]    2    7   12   17
## [66,]    3    8   13   18
## [67,]    4    9   14   19
## [68,]    5   10   15   20

Indexing works:

a[64,]
## [1]  1  6 11 16

Upvotes: 2

Related Questions