0

I have following data and trying change CCG and Pract to numbers so I can use stan or Winbugs...when I try to change it seems its changing the order of the data.. I want to change CCG and Pract to numbers without changing the order of the data...I tried hard but I couldn't do it. I am struggling with this basic issue than writing Bugs codes....please help..

I have the following data

  CCG  pract  Deno Numer Points  Excep 
1 01C N81049   49    46      4       4           
2 01C N81022   28    26      4      23          
3 01C N81632   66    64      4       4          
4 01C N81069   15    14      4       3           
5 01C N81062   98    89      4       9          
6 01C N81033   31    28      4       9          

I tried to change to integer using as.integer() and I am getting I am getting..

  CCG pract Deno Numer Points Excep
1  20  6621  160   144    41    36       
2  20  6594  130   117    41    18       
3  20  6698  179   164    41    36       
4  20  6640   57    46    41    25       
5  20  6633  214   191    41    62       
6  20  6605  137   119    41    62       

By checking Deno and Numer it is clear the order of the data has been changed...Why CCG is not starting from 1?

I want

   CCG  pract  Deno Numer   Points Excep 
1 01C N81049   49    46      4         4           
2 01C N81022   28    26      4        23          
3 01C N81632   66    64      4         4          
4 01C N81069   15    14      4         3           
5 01C N81062   98    89      4         9          
6 01C N81033   31    28      4         9          

change to something like this

  CCG  pract     Deno Numer    Points Excep 
1    1    1      49    46        4       4           
2    1    1      28    26        4      23          
3    1    1      66    64        4       4          
4    1    1      15    14        4       3          
5    1    1      98    89        4       9          
6    1    1      31    28        4       9          

Please help me..

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
Vera
  • 1
  • 1

1 Answers1

2

In R, factors are internally represented as integers, linking to a table of the factor levels. AFAIK, these internal integers are assigned based on a lexicographic order of the factor levels, so 57 gets a higher code than 238.

as.integer() will extract this internal integer coding. As you found out, this is not very useful. (I honestly don't understand why R does this when applying as.integer() to factors that have integers as factor levels.)

Solution: first convert to character, then to integer. as.integer(as.character(Deno))

Stephan Kolassa
  • 7,953
  • 2
  • 28
  • 48