[R] value replacement in matrices

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon Aug 4 10:10:08 CEST 2003


George, Charles R wrote:

> 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]
> }

  replace(originalMat, indexMat[,1], indexMat[,2])

Uwe Ligges


> 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}}
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list