Skip to content
Prev 26085 / 398502 Next

Warning: as.numeric reorders factor data

The behavior makes more sense now but is in need of clarification in the 
help files.  

Specifically, aggregate should mention that it is converting arguments 
to characters.  Factoring a numeric vector leads to what you might 
expect, factors ordered numerically.  So, even though I knew the by 
variables were being factored, it seemed they should be okay.  For instance,

 > factor(c(1:15))
 [1] 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

 Ultimately, it occurred to me after much staring at the output that 
factoring must be doing a character conversion first, and that is how I 
figured out my workaround.  As it turns out, the workaround is  in the 
FAQ (albeit filed under Miscellanea).

So, ultimately the problem may not be in as.numeric.  Since one needs to 
know something about the internal processing of aggregate to use it, it 
might be that this should be in the help files.
Thomas Lumley wrote: