1

我试图绕过stats::reordervs的奇怪重叠Hmisc::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 的情况下获得无序因子?

4

1 回答 1

4

你可以这样做

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

请注意, stats::reorder 是一个“调度程序”,因此在您的示例中

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

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

于 2010-04-27T07:38:09.120 回答