[R] value replacement in matrices

George, Charles R Charles.George at dpi.qld.gov.au
Fri Aug 1 08:22:35 CEST 2003


Hello R users

I would like to ask if anyone knows a computationally fast solution to this problem:

I have an original matrix and an index matrix. The original matrix is ca 4000x4000 cells, and the index matrix has 261 unique values. From these, I want to produce a new matrix.

Consider the following simplified example:

Original matrix
1 4 6 5
3 4 8 5
2 4 7 8
9 8 3 6

index matrix
1 5
2 7
3 2
4 5
5 3
6 7
7 5
8 3
9 9

my current code is something like this

for(i in 1:9) {
	changeVal <- which(originalMat==indexMat[i,1])
	finalMat <- indexMat[i,2]
}

the output would look like this:

Final matrix
5 5 7 3
2 5 3 3
7 5 5 3
9 3 2 7

At the moment it takes a while to process. Does anyone have any suggestions?

reagrds

Robert
 

********************************DISCLAIMER******************...{{dropped}}




More information about the R-help mailing list