2

可能重复:
当在附加(显然)不相关的包之前和之后运行相同的代码时,数据框转换会产生不同的结果

我有点问题。我曾经使用这种方法快速重新排序因子以使用 ggplot2 进行绘图。但它似乎不起作用:

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?

这里发生了什么?

会话信息()

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    
4

1 回答 1

2

gdata定义了一种方法,该方法reorder.factor具有reorder.default应用于因子的不同行为。请参阅this other question中的更详细讨论。

gdata在下面的新会话中,我已经包含了这种行为的记录,有和没有;请注意,一旦加载gdata,就很难摆脱该方法(出于此处探讨的原因)。

> 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
于 2012-10-30T20:05:16.597 回答