[R] replacing elements in matrix: fastest method?

Agustin Lobo alobo at ija.csic.es
Sat Aug 4 10:17:48 CEST 2001


Hi!

I replace some elements of a matrix a

> a
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12

according to a reclassification matrix such

> pares
     [,1] [,2]
[1,]    1    2
[2,]    5    6
[3,]    8    7

to get

> b
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12

As both a and pares can be very large, I'd like to know the
fastest way for this operation in R. I'm using:

a[which(a%in%pares[,1])]<-pares[,2]

and

replace(a,which(ima%in%pares[,1]),pares[,2])

Is there any preference in terms of speed
and/or R style? Is there another, better way?

Thanks

Agus




Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list