[R] Warning: as.numeric reorders factor data
Bud Gibson
fpgibson at umich.edu
Sun Dec 8 15:04:02 CET 2002
Recently, I was using aggregate() to develop averages by trial for an
experiment I was running. Trials were indicated as ordinal numbers for
each subject. aggregate() turned trial into factors during the
aggregation process. I then wanted to create a scatter plot of subject
performance by trial, so I applied as.numeric to the (now) factor
variable trial. as.numeric reordered the trial indicator creating some
(at first) incomprehensible results.
Investigation revealed that aggregate must first be interpreting trial
as a character and then turning it into a factor. The behavior I
observed is reproducible from the following transcript using R1.6.1 on
RH linux 7.3.
> 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(as.character(test))
[1] 1 2 3 4 5 6 7 8 9 10 11
However, I should not have to know about the internals of aggregate to
be able to use its results.
Bud Gibson
More information about the R-help
mailing list