stats::reorder 与 Hmisc::reorder

发布于 2024-08-30 13:38:05 字数 1750 浏览 4 评论 0原文

我试图解决 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< /code> 直接,我现在由于某种原因得到了一个有序因子。

> 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 的情况下获得无序因子?

I am trying to get around the strange overlap of stats::reorder vs Hmisc::reorder.

Without Hmisc loaded I get the result I want, i.e. an unordered factor:

> 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

Now after loading Hmisc the result is an ordered factor:

> 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

In calling stats::reorder directly, I now for some reason get an ordered factor.

> 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

Specifying, that I would need an unordered factor results in an error suggesting that stats::reorder is not used?

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

So the question really is how do I get an unordered factor with Hmisc loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2024-09-06 13:38:05

您可以通过

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

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

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

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

you can do by

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

note that stats::reorder is a 'dispatcher' so in your example

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

finally Hmisc::reorder.factor is called instead of stats::reorder.default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文