top_n函数返回的行比预期的多 [英] top_n function returning more rows than expected

查看:108
本文介绍了top_n函数返回的行比预期的多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是r的新手(很累,我想我的大脑目前无法正常工作),但是对我来说,以下代码应该只返回10行-返回66。为什么呢?

I'm pretty new to r (and pretty tired- I imagine my brain is just not currently working properly) but to me the below code should only return 10 rows- it returns 66. Why is this?

library(dplyr)

a <- structure(list(calls_in_range = c(17, 14, 6, 4, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), mid_point = c(-20680, 
-20660, -20640, -20700, -36900, -36500, -36380, -36020, -35960, 
-35260, -35140, -34640, -33060, -32600, -30920, -29340, -29100, 
-28780, -27980, -27640, -27220, -27160, -26980, -26800, -26740, 
-26500, -25640, -25540, -24840, -24820, -24800, -24380, -23880, 
-23820, -23720, -23320, -22220, -21920, -21860, -21760, -21060, 
-20240, -19780, -18700, -18500, -17500, -16740, -16500, -14260, 
-14200, -14120, -13860, -13420, -13120, -12780, -12740, -12460, 
-12420, -12280, -12260, -11720, -10660, -10060, -9960, -6380, 
-5520), lower_range = c(-20690, -20670, -20650, -20710, -36910, 
-36510, -36390, -36030, -35970, -35270, -35150, -34650, -33070, 
-32610, -30930, -29350, -29110, -28790, -27990, -27650, -27230, 
-27170, -26990, -26810, -26750, -26510, -25650, -25550, -24850, 
-24830, -24810, -24390, -23890, -23830, -23730, -23330, -22230, 
-21930, -21870, -21770, -21070, -20250, -19790, -18710, -18510, 
-17510, -16750, -16510, -14270, -14210, -14130, -13870, -13430, 
-13130, -12790, -12750, -12470, -12430, -12290, -12270, -11730, 
-10670, -10070, -9970, -6390, -5530), upper_range = c(-20670, 
-20650, -20630, -20690, -36890, -36490, -36370, -36010, -35950, 
-35250, -35130, -34630, -33050, -32590, -30910, -29330, -29090, 
-28770, -27970, -27630, -27210, -27150, -26970, -26790, -26730, 
-26490, -25630, -25530, -24830, -24810, -24790, -24370, -23870, 
-23810, -23710, -23310, -22210, -21910, -21850, -21750, -21050, 
-20230, -19770, -18690, -18490, -17490, -16730, -16490, -14250, 
-14190, -14110, -13850, -13410, -13110, -12770, -12730, -12450, 
-12410, -12270, -12250, -11710, -10650, -10050, -9950, -6370, 
-5510)), class = "data.frame", row.names = c(NA, -66L), .Names = c("calls_in_range", 
"mid_point", "lower_range", "upper_range"))

top_n(a, 10, calls_in_range)


推荐答案

如果检查 calls_in_range 列,则可以看到有联系。这是用于订购的变量。根据文档中的 top_n 函数中的> n 参数:

If you inspect the calls_in_range column, you can see there are ties. This is the variable used for ordering. According to the documentation for the n argument in the top_n function:


要返回的行数。如果将x分组,则这是每组的行数。 如果有关系,将包括多于n行。如果n为正,则选择前n行。如果为负,则选择底部的n行。

number of rows to return. If x is grouped, this is the number of rows per group. Will include more than n rows if there are ties. If n is positive, selects the top n rows. If negative, selects the bottom n rows.

这就是为什么它返回比预期更多的行的原因。

This is why it is returning more rows than expected.

这篇关于top_n函数返回的行比预期的多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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