top_n与r中的顺序 [英] top_n versus order in r

查看:196
本文介绍了top_n与r中的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解dplyr的top_n函数的输出。有人可以帮忙吗?

I am having trouble understanding the output from dplyr's top_n function. Can anybody help?

n=10

df = data.frame(ref=sample(letters,n),score=rnorm(n))

require(dplyr)

print(dplyr::top_n(df,5,score))

print(df[order(df$score,decreasing = T)[1:5],])

top_n 的输出未按我预期的分数排序。与使用 order 函数

The output from top_n is not ordered according to score as I expected. Compare with using the order function


 ref      score
1   i 0.71556494
2   p 0.04463846
3   v 0.37290990
4   g 1.53206194
5   f 0.86307107
   ref      score
7    g 1.53206194
10   f 0.86307107
1    i 0.71556494
6    v 0.37290990
4    p 0.04463846

我阅读的文档也暗示 top_n 结果应按指定的列排序,例如

The documentation I have read also implies the top_n results should be ordered by the specified column, for example

https://www.rstudio.com/wp-content/uploads/2015/02/data- wrangling-cheatsheet.pdf

推荐答案

我的误解和期望是由于我阅读了与问题并在评论中进行了描述。尽管有一些文档要求,但 top_n 不会生成按 wt 排序的输出。

My misunderstanding and expectation was due to my reading of the documentation linked to in the question and described in the comments. Despite some documentation claims, top_n does not generated output ordered by wt.

这篇关于top_n与r中的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆