stats :: reorder vs Hmisc :: reorder

时间:2010-04-27 06:57:51

标签: r r-factor

我正试图绕过stats::reorderHmisc::reorder的奇怪重叠。

如果没有Hmisc加载,我会得到我想要的结果,即无序因素:

> with(InsectSprays, reorder(spray, count, median))
 [1] A A A A A A A A A A A A B B B B B B B B B B B B C C C C C C C C C C C C D D
[39] D D D D D D D D D D E E E E E E E E E E E E F F F F F F F F F F F F
attr(,"scores")
   A    B    C    D    E    F 
14.0 16.5  1.5  5.0  3.0 15.0 
Levels: C E D A F B

现在加载Hmisc后,结果是一个有序因子:

> library(Hmisc)
Loading required package: survival
Loading required package: splines

Attaching package: 'Hmisc'

The following object(s) are masked from 'package:survival':

    untangle.specials

The following object(s) are masked from 'package:base':

    format.pval, round.POSIXt, trunc.POSIXt, units

> with(InsectSprays, reorder(spray, count, median))
 [1] A A A A A A A A A A A A B B B B B B B B B B B B C C C C C C C C C C C C D D
[39] D D D D D D D D D D E E E E E E E E E E E E F F F F F F F F F F F F
Levels: C < E < D < A < F < B

直接致电stats::reorder,我现在出于某种原因获得有序因素。

> with(InsectSprays, stats::reorder(spray, count, median))
 [1] A A A A A A A A A A A A B B B B B B B B B B B B C C C C C C C C C C C C D D
[39] D D D D D D D D D D E E E E E E E E E E E E F F F F F F F F F F F F
Levels: C < E < D < A < F < B

指定我需要一个无序因素会导致错误,表明未使用stats::reorder

> with(InsectSprays, stats::reorder(spray, count, median, order = FALSE))
Error in FUN(X[[1L]], ...) : unused argument(s) (order = FALSE)

所以问题确实是如何在加载Hmisc的情况下获得无序因素?

1 个答案:

答案 0 :(得分:4)

你可以做到

with(InsectSprays, stats:::reorder.default(spray, count, median))

请注意,stats :: reorder是一个'调度程序',所以在你的例子中

with(InsectSprays, stats::reorder(spray, count, median))

最后调用Hmisc :: reorder.factor而不是stats :: reorder.default。