R: Vector to matrix - convert one-dimensional vector index to two-dimensional matrix index?

Let's say I have a vector:

myVector <- c(1,2,3,4,1,2,3,4,1,2,3,4)

And for some reason index, say, 9 in that vector (having value 1) is important to me:

> myVector[9]
[1] 1

For some another reason, I want to make this vector a matrix with dimensions 6x2 (six rows, two columns).

> myMatrix <- matrix(myVector, nrow = 6, ncol = 2)  # Only either nrow or ncol argument is actually required.
> myMatrix
     [,1] [,2]
[1,]    1    3
[2,]    2    4
[3,]    3    1
[4,]    4    2
[5,]    1    3
[6,]    2    4

Now I would like to know where my vector index 9 is located at in this new matrix. How do I get that information?

Of course, I can see in this case that it is row number 3 and column number 2, but how do I know in general where do the parameters of the transformation (number of rows and columns in the matrix) take my original index?

Upvotes: 2

Views: 743

Answers (3)

lroha
lroha

Reputation: 34291

You can use arrayInd():

arrayInd(9, .dim = dim(myMatrix))

     [,1] [,2]
[1,]    3    2

Upvotes: 2

user12256545
user12256545

Reputation: 3002

The matrix you are creating still incorporates the vector it was build on, so indexing should still work.

myVector <-seq(10,120,10)
myMatrix <- matrix(myVector, nrow = 6, ncol = 2)
# check
myMatrix[3,2]
myVector[9]

# check all positions:
all(myMatrix==myVector)

Addition:

To convert the singular array index to a pair of indices i,j I wrote this function, [maybe there is a more elegant solution to this problem.]

Take the singular index v_i and apply a modulo operation with first dimension i of the matrix M , the edge-case would be that the result is 0, than we use i instead the modulo result. [This is implemented with the ifelse statement.]

For the second index j , v_i is simply divided by i and round op to the next integer.

# This small functions converts the singular array index to
# a matrix index i,j based on the dimensions of the input matrix
M_ind <- function(v_i,M) {
  i = dim(M)[1]
  i_m = ifelse(v_i%%i,v_i%%i,i)
  return(c(i_m,ceiling(v_i/i)))
}

## test
test<-M_ind(7,myMatrix)
test
myMatrix[test[1],test[2]]

## Another matrix
myVec<-LETTERS[1:24]
myMat<-matrix(myVec,4,6)

myVec[16] # "P"
test<-M_ind(16,myMat)
test
myMat[test[1],test[2]] #"P"

Upvotes: 0

After giving myself some time to think this, I figured out that this can be solved with some algebra:

originalIndex <- 9 # this is the index in the original vector
nrows <- 6 # number of rows in the matrix to be created

rowIndex = originalIndex - (ceiling(originalIndex / nrows) - 1) * nrows
columnIndex = ceiling(originalIndex / nrows) 

And the print out is as it should:

> rowIndex
[1] 3
> columnIndex
[1] 2

Upvotes: 2

Related Questions