变换和重新排序

时间:2012-10-30 19:46:49

标签: r

  

可能重复:
  Data frame transformation gives different results when same code is run before and after attaching (apparently) unrelated packages

我遇到了一些问题。我曾经使用这种方法快速重新排序与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?

这里发生了什么?

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    

1 个答案:

答案 0 :(得分:2)

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

我已经在下面的新会话中提供了此行为的记录,包括和不包含gdata;请注意,一旦加载gdata,就很难摆脱该方法(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