Skip to content

Mapping strings to integers

3 messages · Ali.Abbas, PIKAL Petr

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

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.
#
Hi
asking
string
its,
with
to
am
Hope
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
http://www.R-project.org/posting-guide.html