-3

I have huge data set. The columns contain values like A,B,C,D,E,F,G,H and I need to replace them with 1,2,3,4...

[1] "C" "C" "C" "C" "C" "A" "H" "G" "G" "G" "G" "G" "G" "G" "C" "C" "C" "C" "C"
[20] "C" "B" "B" "B" "H" "H" "H" "H" "H" "H" "G" "C" "A" "A" "A" "A" "A" "A" "A"
[30]----

Another similar problem is values in one column are more than 1000 and I need to replace them by unique numbers.

josliber
  • 43,891
  • 12
  • 98
  • 133
Sudas
  • 41
  • 8
  • 1
    change to factors and convert to numeric (`as.numeric(factor(LETTERS))`) works for your example case – rawr Jun 09 '15 at 15:33

1 Answers1

0

try replace

replace function examples

in your case e.g.

replace(df, "A", 1) 
Community
  • 1
  • 1
drsh1
  • 54
  • 7
  • what about when there is large number of unique values that need to change with unique number? – Sudas Jun 09 '15 at 15:22
  • please provide reproducible example – drsh1 Jun 09 '15 at 15:56
  • my the data frame is like this which contains different values but it is so huge some portion is like this df= [1] "C01" "C02" "C01" "C05" "C04" "A01" "H10" "G10" "G30" "G12" "G10" "G01" "G20" "G30" "C50" "C50" "C32" "C43" "C10"
    [20] "C01" "B03" "B01" "B01" .... output could be like this but doest have to be, because one particular value has one unique number for example for "C01" could be 301 "C02" could be 302 and so on df= [1] "301" "302" "301" "305" "304" "101" "810" "710" "730" "712" "710" "701" "720" "730" "350" "350" "332" "343" "310"
    [20] "301" "203" "201" "201" ....
    – Sudas Jun 09 '15 at 17:06