Hi! 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 cbind(c(1,2,3),c(1001,1002,1003)) 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] v } Agus -- Dr. Agustin Lobo Institut de Ciencies de la Terra "Jaume Almera" (CSIC) LLuis Sole Sabaris s/n 08028 Barcelona Spain Tel. 34 934095410 Fax. 34 934110012 email: Agustin.Lobo at ija.csic.es http://www.ija.csic.es/gt/obster
Dear Agustin, Perhaps v1 <- c(1,1,1,2,3,4,1,10,3) dput(as.numeric(ifelse(v1%in%c(1,2,3),paste(100,v1,sep=""),v1))) HTH, Jorge On Fri, Jun 27, 2008 at 2:41 PM, Agustin Lobo <aloboaleu@gmail.com> wrote:> Hi! > > 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 > cbind(c(1,2,3),c(1001,1002,1003)) > > 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] > v > } > > Agus > > -- > Dr. Agustin Lobo > Institut de Ciencies de la Terra "Jaume Almera" (CSIC) > LLuis Sole Sabaris s/n > 08028 Barcelona > Spain > Tel. 34 934095410 > Fax. 34 934110012 > email: Agustin.Lobo@ija.csic.es > http://www.ija.csic.es/gt/obster > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
if there's nothing specific for it, you could probably do it with merge? on 06/27/2008 02:41 PM Agustin Lobo said the following:> Hi! > > 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 > cbind(c(1,2,3),c(1001,1002,1003)) > > 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] > v > } > > Agus >
on 06/27/2008 01:41 PM Agustin Lobo wrote:> Hi! > > 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 > cbind(c(1,2,3),c(1001,1002,1003)) > > 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] > v > } > > Agus >v1 <- c(1,1,1,2,3,4,1,10,3) dic <- cbind(c(1,2,3),c(1001,1002,1003)) Ind <- match(v1, dic[, 1]) > Ind [1] 1 1 1 2 3 NA 1 NA 3 > sapply(seq(along = Ind), function(x) if (is.na(Ind[x])) v1[x] else dic[Ind[x], 2]) [1] 1001 1001 1001 1002 1003 4 1001 10 1003 I would certainly test that a bit more to be sure it works on other data. HTH, Marc Schwartz
Thanks. This is a simple and efficient solution for the case in which the elements of the vector "values" are integers (which is often the case as in the example that came to my mind). Nevertheless, let me suggest having a more comprehensive function recode in base, as this is a very usual and well defined operation. Agus Phil Spector wrote:> Agustin - > One way to do it is with named vectors: > >> codes = c(1001,1002,1003) >> names(codes) = c(1,2,3) >> values = c(1,1,1,2,3,4,1,10,3) >> newvalues = codes[values] >> newvalues > 1 1 1 2 3 <NA> 1 <NA> 3 > 1001 1001 1001 1002 1003 NA 1001 NA 1003 > > Since NAs are placed wherever there wasn't a value in codes, > those values need to be restored: > >> newvalues[is.na(newvalues)] = values[is.na(newvalues)] >> newvalues > 1 1 1 2 3 <NA> 1 <NA> 3 > 1001 1001 1001 1002 1003 4 1001 10 1003 > > > - Phil Spector > Statistical Computing Facility > Department of Statistics > UC Berkeley > spector at stat.berkeley.edu > > > > On Fri, 27 Jun 2008, Agustin Lobo wrote: > >> Hi! >> >> 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 >> cbind(c(1,2,3),c(1001,1002,1003)) >> >> 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] >> v >> } >> >> Agus >> >> -- >> Dr. Agustin Lobo >> Institut de Ciencies de la Terra "Jaume Almera" (CSIC) >> LLuis Sole Sabaris s/n >> 08028 Barcelona >> Spain >> Tel. 34 934095410 >> Fax. 34 934110012 >> email: Agustin.Lobo at ija.csic.es >> http://www.ija.csic.es/gt/obster >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >-- Dr. Agustin Lobo Institut de Ciencies de la Terra "Jaume Almera" (CSIC) LLuis Sole Sabaris s/n 08028 Barcelona Spain Tel. 34 934095410 Fax. 34 934110012 email: Agustin.Lobo at ija.csic.es http://www.ija.csic.es/gt/obster