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) Thx, Naira -- View this message in context: http://www.nabble.com/From-strings-to-numbers-tp17315179p17315179.html Sent from the R help mailing list archive at Nabble.com.
On 5/19/2008 6:13 AM, 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)X <- c("ab","ab","bb","cat","cat","ab") as.numeric(as.factor(X)) [1] 1 1 2 3 3 1> Thx, > Naira-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
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