[R] Recoding

Agustin Lobo aloboaleu at gmail.com
Fri Jun 27 20:41:14 CEST 2008


Given a vector (or a factor within a df),i.e. v1 <- c(1,1,1,2,3,4,1,10,3)
and a dictionary

is there a function (on the same line than recode() in car)
to get v2 as c(1001,1001,1001,1002,1003,4,1001,10,1003) ?

I'm using myself a function based on match() since
long ago (I think that thanks to advice by Prof. B. Ripley),
but would like to know if there is an standard function (i.e., like
recode()). What I'm using is:

"reclas" <- function(v, origen, imagen, directo = T, resto=1)
         if(directo == F) {
         	aux <- origen
                 origen <- imagen
                 imagen <- aux
         m <- match(v, origen, 0)
         #print("match finished")
         if(resto==0) v <- v*0
         v[m > 0] <- imagen[m]


Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Tel. 34 934095410
Fax. 34 934110012
email: Agustin.Lobo at ija.csic.es

More information about the R-help mailing list