[R] From strings to numbers

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 19 16:18:49 CEST 2008


On Mon, May 19, 2008 at 7:41 AM, Naira Naouar <nanao at psb.ugent.be> wrote:
> 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.

The responses you got do not depend on the internal representation of
factors but using unclass does depend on that.




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



More information about the R-help mailing list