[R] Warning: as.numeric reorders factor data

Bud Gibson fpgibson at umich.edu
Sun Dec 8 16:01:02 CET 2002


Thanks for the clarification.  It's nice to know that there is some 
systematicity to the behavior.

Is this documented anywhere?  I did look at the help for as.numeric, and 
it makes no mention that it is coercing factors based on their level. 
 This may be obvious to those deeply immersed in R and its machinations, 
but to those who think the number they see on the screen should just 
become a number when it is coerced to one, it is disconcerting.

Further, if I just factor the same vector, and then coerce it back to 
numeric, the order I would have expected is preserved.  I did not report 
that test because it seemed irrelevant.  Why isn't aggregate just doing 
that?

My cut is that there should be some warning in the documentation, 
perhaps in aggregate, about the specific assumptions used in making 
implicit transformations and what one can expect.

Peter Dalgaard BSA wrote:

>Bud Gibson <fpgibson at umich.edu> writes:
>
>  
>
>> > test <- as.factor(as.character(c(1,2,3,4,5,6,7,8,9,10,11)))
>> > test
>>  [1] 1  2  3  4  5  6  7  8  9  10 11
>>Levels: 1 10 11 2 3 4 5 6 7 8 9
>> > as.numeric(test)
>>  [1]  1  4  5  6  7  8  9 10 11  2  3
>>
>>It strikes me that as.numeric should *never* reorder the vector it is
>>working on.  There is this workaround for the problem:
>>    
>>
>
>as.numeric is not reordering anything. "2" is the 4th level of the
>test factor, which in turn is due to alphabetic ordering of the
>factor levels in as.factor() [or factor() for that matter]. If you
>want to avoid that, set factor levels explicitly:
>
>test <- factor(as.character(c(1:11)),levels=c(1:11))
>test
>as.numeric(test)
>
>I suppose that similar treatment of your "trial" variable prior to 
>calling aggregate() could solve your problem.
>
>  
>




More information about the R-help mailing list