重新排序因子给出不同的结果,具体取决于加载的包

标签 r namespaces masking operator-precedence

我想创建一个条形图,其中条形图是按高度而不是按类别的字母顺序排列的。当我加载的唯一包是 ggplot2 时,这很好用。但是,当我加载更多包并运行创建、排序和绘制数据框的相同代码时,条形又恢复为按字母顺序排序。

我每次都使用 str() 检查数据框结果发现数据框的属性现在不同了,即使我每次都运行相同的代码。

下面列出了我的代码和输出。谁能解释不同的行为?为什么加载一些明显不相关的包(在我使用的任何功能似乎都没有被新加载的包掩盖的意义上不相关)会改变运行 transform() 的结果功能?

案例 1:只加载了 ggplot2

library(ggplot2)

group = c("C","F","D","B","A","E")
num = c(12,11,7,7,2,1)
data = data.frame(group,num)
data1 = transform(data, group=reorder(group,-num))

> str(data1)
'data.frame':   6 obs. of  2 variables:
 $ group: Factor w/ 6 levels "C","F","B","D",..: 1 2 4 3 5 6
  ..- attr(*, "scores")= num [1:6(1d)] -2 -7 -12 -7 -1 -11
  .. ..- attr(*, "dimnames")=List of 1
  .. .. ..$ : chr  "A" "B" "C" "D" ...
 $ num  : num  12 11 7 7 2 1

案例2:加载多个包,然后再次运行相同的代码
library(plyr)
library(xtable)
library(Hmisc)
library(gmodels)
library(reshape2)
library(vcd)
library(lattice)

group = c("C","F","D","B","A","E")
num = c(12,11,7,7,2,1)
data = data.frame(group,num)
data1 = transform(data, group=reorder(group,-num))

> str(data1)
'data.frame':   6 obs. of  2 variables:
 $ group: Factor w/ 6 levels "A","B","C","D",..: 3 6 4 2 1 5
 $ num  : num  12 11 7 7 2 1

更新:SessionInfo()

案例 1:加载 ggplot2 后运行 sessionInfo()
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
  [1] C/en_US.UTF-8/C/C/C/C

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

other attached packages:
  [1] ggplot2_0.9.1

loaded via a namespace (and not attached):
  [1] MASS_7.3-18        RColorBrewer_1.0-5 colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.0       
[7] labeling_0.1       memoise_0.1        munsell_0.3        plyr_1.7.1         proto_0.3-9.2      reshape2_1.2.1    
[13] scales_0.2.1       stringr_0.6        tools_2.15.0

案例 2:加载附加包后运行 sessionInfo()
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
  [1] C/en_US.UTF-8/C/C/C/C

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

other attached packages:
  [1] lattice_0.20-6   vcd_1.2-13       colorspace_1.1-1 MASS_7.3-18      reshape2_1.2.1   gmodels_2.15.2  
[7] Hmisc_3.9-3      survival_2.36-14 xtable_1.7-0     plyr_1.7.1       ggplot2_0.9.1   

loaded via a namespace (and not attached):
  [1] RColorBrewer_1.0-5 cluster_1.14.2     dichromat_1.2-4    digest_0.5.2       gdata_2.8.2        gtools_2.6.2      
[7] labeling_0.1       memoise_0.1        munsell_0.3        proto_0.3-9.2      scales_0.2.1       stringr_0.6       
[13] tools_2.15.0

最佳答案

发生这种情况是因为:

  • gmodels进口gdata
  • gdatareorder.factor 创建一个新方法

  • 开始一个干净的 session 。然后:
    methods("reorder")
    [1] reorder.default*    reorder.dendrogram*
    

    现在加载 gdata (或加载 gmodels ,具有相同的效果):
    library(gdata)
    methods("reorder")
    [1] reorder.default*    reorder.dendrogram* reorder.factor 
    

    注意这里没有屏蔽,因为 reorder.factor基地中不存在

    重新创建问题,但这次明确调用不同的包:
    group = c("C","F","D","B","A","E")
    num = c(12,11,7,7,2,1)
    data = data.frame(group,num)
    

    基本 R 版本(使用 reorder.default):
    str(transform(data, group=stats:::reorder.default(group,-num)))
    'data.frame':   6 obs. of  2 variables:
     $ group: Factor w/ 6 levels "C","F","B","D",..: 1 2 4 3 5 6
      ..- attr(*, "scores")= num [1:6(1d)] -2 -7 -12 -7 -1 -11
      .. ..- attr(*, "dimnames")=List of 1
      .. .. ..$ : chr  "A" "B" "C" "D" ...
     $ num  : num  12 11 7 7 2 1
    
    gdata版本(使用 reorder.factor ):
    str(transform(data, group=gdata:::reorder.factor(group,-num)))
    'data.frame':   6 obs. of  2 variables:
     $ group: Factor w/ 6 levels "A","B","C","D",..: 3 6 4 2 1 5
     $ num  : num  12 11 7 7 2 1
    

    关于重新排序因子给出不同的结果,具体取决于加载的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10939516/

    相关文章:

    r - 遍历文件夹层次结构

    python - 列表的 boolean 掩码作为 Pandas 数据框中的条目

    r - R 数据表中平均值的偏差

    r - CFA 漂亮的路径图

    c# - 在大型项目中使用命名空间是否有任何指导方针?

    python - python子模块如何在它们之间共享稀缺资源?

    python - 带有用于可变长度输入的屏蔽层的 Keras lstm

    iOS 在 drawRect 中反转蒙版

    R 版本 4.0.0 上的 ROracle

    linux - PyRoute2::Psutil 等效?