[R] From strings to numbers

Jim Lemon jim at bitwrit.com.au
Mon May 19 13:36:41 CEST 2008


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



More information about the R-help mailing list