[R] From strings to numbers

Naira Naouar nanao at psb.ugent.be
Mon May 19 13:41:38 CEST 2008


Jim and Chuck,

Thanks a lot for your replies :)
I knew that there was a better way than the complex thing I was planning 
to apply :p (with a lot of for/while/apply arghhh)
I also remembered to use factor when my brain woke up ;)

I did something like unclass(factor(c("ab","ab","bb","cat","cat","ab"))) 
which is also doing the right job.
Thx a lot for your fast replies.

Naira

Jim Lemon wrote:
> Naira wrote:
>> Dear all,
>>
>> I would like to know if there is an easy to transform a vector of 
>> strings to
>> a vector of integers.
>> Ex: ("ab","ab","bb","cat","cat","ab") will be
>> (1, 1, 2, 3, 3, 1)
>>
> Hi Naira,
> It's not all that hard...
>
> newfactor<-as.factor(c("ab","ab","bb","cat","cat","ab"))
>  newfactor
> [1] ab  ab  bb  cat cat ab
> Levels: ab bb cat
> as.numeric(newfactor)
> [1] 1 1 2 3 3 1
>
> Jim


-- 
==================================================================
Naira Naouar 

Tel:+32 (0)9 331 38 63
VIB Department of Plant Systems Biology, Ghent University
Technologiepark 927, 9052 Gent, BELGIUM
nanao at psb.ugent.be                         http://www.psb.ugent.be



More information about the R-help mailing list