2

Possible Duplicate:
Data frame transformation gives different results when same code is run before and after attaching (apparently) unrelated packages

I'm having a bit of a problem. I used to use this method to quickly reorder factors for plotting with ggplot2. But it doesn't seem to be working:

tmp <- data.frame(Letters=letters[1:26],values=rnorm(26))
tmp <- transform(tmp, Letters = reorder(Letters,values))

identical(levels(tmp$Letters),letters[1:26]) # True! but shouldn't it be reorderd?

What's going on here?

SessionInfo()

R version 2.15.2 (2012-10-26)
Platform: i686-pc-linux-gnu (32-bit)

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8       
 [4] LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] tools     stats     graphics  grDevices utils     methods   base     

other attached packages:
[1] reshape_0.8.4   digest_0.5.2    plyr_1.7.1      ggplot2_0.9.2.1

loaded via a namespace (and not attached):
 [1] brew_1.0-6         colorspace_1.2-0   dichromat_1.2-4    evaluate_0.4.2     grid_2.15.2       
 [6] gtable_0.1.1       gtools_2.7.0       httr_0.2           labeling_0.1       MASS_7.3-22       
[11] memoise_0.1        munsell_0.4        parallel_2.15.2    proto_0.3-9.2      RColorBrewer_1.0-5
[16] RCurl_1.95-1.1     reshape2_1.2.1     scales_0.2.2       stringr_0.6.1      whisker_0.1    
Community
  • 1
  • 1
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255

1 Answers1

2

gdata defines a method reorder.factor which has a different behavior that reorder.default applied to factors. See a more detailed discussion in this other question.

I've included a transcript of this behavior, with and without gdata, starting in a new session below; note that once you have loaded gdata, it is very difficult to get rid of that method (for reasons explored here).

> tmp <- data.frame(Letters=letters[1:26],values=rnorm(26))
> tmp <- transform(tmp, Letters = reorder(Letters,values))
> 
> identical(levels(tmp$Letters),letters[1:26])
[1] FALSE
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
> 
> library("gdata")
gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.

gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.

Attaching package: ‘gdata’

The following object(s) are masked from ‘package:stats’:

    nobs

The following object(s) are masked from ‘package:utils’:

    object.size

> 
> tmp <- data.frame(Letters=letters[1:26],values=rnorm(26))
> tmp <- transform(tmp, Letters = reorder(Letters,values))
> 
> identical(levels(tmp$Letters),letters[1:26])
[1] TRUE
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gdata_2.12.0

loaded via a namespace (and not attached):
[1] gtools_2.7.0
Community
  • 1
  • 1
Brian Diggs
  • 57,757
  • 13
  • 166
  • 188