[R] Odp: Mapping strings to integers

Petr PIKAL petr.pikal at precheza.cz
Tue Aug 23 17:18:48 CEST 2011


Hi

> Dear All,
> I am quite a newbie to R. Trying to learn it these days. Sorry for 
asking
> such a basic question, but could you kindly tell me how to map unique 
string
> values to integers in a data frame? I have a graph which has, most of 
its,
> vertices' attributes in a string format. I would like to replace them 
with
> ascending integers. For example, first unique value in column 1 changes 
to
> "0". I'd like to have it so that I could use my computer's resources
> efficiently and also could easily perform statistical measures on it. I 
am
> looking for a two way mapping, so that, after analysis, I could see
> meaningful results in terms of the same but in reverse order mappings. 
Hope
> I was clear in my question.

I doubt it. Maybe others can resolve your intention without any data or 
code. I can only guess that you have some character values in some king of 
object (data.frame?, vector?, matrix?....0

basically you can change it to factor and do as.numeric to this factor. 
Something like

x<-sample(letters[1:5], 20, replace=TRUE)
 [1] 5 4 4 2 5 3 4 5 5 3 3 2 5 3 3 3 4 1 4 3
data.frame(x=x, x.n=as.numeric(factor(x)))
   x x.n
1  e   5
2  d   4
3  d   4
4  b   2
5  e   5
6  c   3
7  d   4
8  e   5
9  e   5
10 c   3
11 c   3
12 b   2
13 e   5
14 c   3
15 c   3
16 c   3
17 d   4
18 a   1
19 d   4
20 c   3

Regards
Petr

> 
> Thanks!
> 
> Cheers,
> Ali
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Mapping-
> strings-to-integers-tp3762828p3762828.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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