[R] convert data

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Dec 4 18:17:07 CET 2003


Muhammad Subianto <subianto at cs.uu.nl> writes:

> Dear R-helper,
> I have a data set like:
> 
> OLDa
> ALL
> OLDc
> OLDa
> OLDb
> NEW
> OLDb
> OLDa
> ALL
> . . .
> ALL
> OLDc
> NEW
> 
> I want to convert that data as OLDa=1, OLDb=2, OLDc=3, NEW=4 and ALL=5
> or the result like:
> 
> 1
> 5
> 3
> 1
> 2
> 4
> 2
> 1
> 5
> . . .
> 5
> 3
> 4
> 
> How can I do it. Thanks you for your help.


I'd do it like this:

> x <- scan(what="")
1: OLDa
2: ALL
3: OLDc
4: OLDa
5: OLDb
6: NEW
7: OLDb
8: OLDa
9: ALL
10:
Read 9 items
> f <- factor(x,levels=c("OLDa", "OLDb", "OLDc", "NEW", "ALL") )
> as.integer(f)
[1] 1 5 3 1 2 4 2 1 5


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list