[R] aggregate() function, strange behavior for augmented data

David Afshartous dafshartous at med.miami.edu
Mon Jun 16 17:30:17 CEST 2008



All,

I'm re-running some analysis that has been augmented with additional data.
When I use the exact same code for the augmented data, the behavior of the
aggregate function is very strange, viz., one of the resulting variables is
now coded as a factor while it was coded as numeric for the original data.
Unfortunately, I cannot provide a reproducible code example since it only
seems to occur with this data.  I've checked and re-checked the of both the
original and augmented data but nothing appears inconsistent.  Any
suggestions much appreciated.  See below for specifics.

Cheers,
David









# original data
> dim(junk1)
[1] 96  3
> junk1[1,]
  Hour Drug Aldo
1    0    P    9
> junk1$Hour
 [1] 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3
5 0 3
[39] 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0
3 5 0
[77] 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5   ### Not coded as a factor
> junk1.mean.time.drug = aggregate(junk1[3], junk1[c(1,2)], mean)
> junk1.mean.time.drug$Hour
[1] 0 3 5 0 3 5  ### not coded as a factor

# augmented data
 dim(junk1)
[1] 108   3
> junk1[1,]
  Hour Drug Aldo
1    0    P    9
> junk1$Hour
  [1] 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3
5 0 3 5 0 3 5 0 3 5 0 3 5 0 3
 [51] 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0
3 5 0 3 5 0 3 5 0 3 5 0 3 5 0
[101] 3 5 0 3 5 0 3 5    ### not coded as a factor
> junk1.mean.time.drug = aggregate(junk1[3], junk1[c(1,2)], mean)
> junk1.mean.time.drug$Hour
[1] 0 3 5 0 3 5
Levels: 0 3 5    ################## coded as a factor now!

## of course, I get recode it again but I'm curious as to why this is
## changing here



More information about the R-help mailing list