[R] convert data

Peter Wolf s-plus at wiwi.uni-bielefeld.de
Fri Dec 5 09:19:24 CET 2003


 > y  <- c( "OLDa", "ALL",  "OLDc", "OLDa", "OLDb", "NEW", "OLDb", 
"OLDa", "ALL","...")
 > el <- c("OLDa", "OLDb", "OLDc", "NEW", "ALL")
 > match(y,el)
 [1]  1  5  3  1  2  4  2  1  5 NA

Peter Wolf

-----------------------

Peter Dalgaard wrote:

>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
>
>
>  
>




More information about the R-help mailing list