Re: [R] Recoding

From: <jgarcia_at_ija.csic.es>
Date: Sat, 28 Jun 2008 09:30:43 +0200 (CEST)

Agustín;

also you can do:

> v <- c(1,1,1,2,3,4,1,10,3)
> dict <- cbind(c(1,2,3),c(1001,1002,1003))
> v <- ifelse(!is.na(match(v,dict)),dict[match(v,dict),2],v)
> v

[1] 1001 1001 1001 1002 1003 4 1001 10 1003

Javier

> 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_at_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_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
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Department of Water and Soil Conservation CEBAS-CSIC
Campus de Espinardo
PO BOX 164
30100 Espinardo (Murcia)
Spain
Tel: +34 630 20 77 58
Fax: +34 968 39 62 13

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. Received on Sat 28 Jun 2008 - 07:30:23 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 28 Jun 2008 - 07:31:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.