Re: [R] From strings to numbers

From: Naira Naouar <nanao_at_psb.ugent.be>
Date: Mon, 19 May 2008 13:41:38 +0200

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@psb.ugent.be                         http://www.psb.ugent.be

______________________________________________
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 Mon 19 May 2008 - 12:28:12 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 Mon 19 May 2008 - 15:00:40 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.

list of date sections of archive