[R] Warning: as.numeric reorders factor data
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Sun Dec 8 15:31:02 CET 2002
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.
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list