对于R的dplyr切片有奇怪的行为 [英] Weird behavior in dplyr slice for R

查看:92
本文介绍了对于R的dplyr切片有奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R的dplyr包中调用 slice(df,i)时,如果我要求的行索引不存在( nrows < i ),它似乎返回所有行,但是返回组中的第一行,就像我已经调用 slice(df,-1)

When calling slice(df, i) in the dplyr package for R, if the row index I ask for doesn't exist (nrows < i), it appears to return all the rows but the first of the group, like I had called slice(df, -1).

例如:

library(dplyr)

c1 <- c("a","b","c")
c2 <- 1:3
df <- data.frame(c1,c2)

slice(df,2)

结果将如预期:

b  2

但是如果我打电话

slice(df, 5)

结果是每行,但第一行:

b  2
c  3

这是特别令人讨厌的使用 group_by(),然后在组上调用 slice()。是否有理由为什么 slice()正在这样做?

This is especially irksome when using group_by() and THEN calling slice() on the groups. Is there a logical reason why slice() is doing this?

似乎返回的行已填充对于行索引大于nrows的NAs,不够高的NAs可以产生所请求的切片可能是有用的结果。

It seems like returning row(s) filled with NAs for row indices larger than 'nrows' in groups not "tall enough" to produce the requested slice could be a useful result.

这是我试图提取每个组的排名结果,但有些组没有足够的数据,而其他组没有。例如列出了每个地区销售量第10位的销售人员。但是在其中一个地区,只有8名销售人员。

This came up as I was trying to extract a ranked result from each group, but some groups did not have enough data while others did. e.g. "List the 10th highest sales-producing salesperson from each region." But in one of the regions there are only 8 salespersons.

推荐答案

我晚点参加了这个聚会,但是在这里。有一个非常简单的解决方案的错误消息错误:不兼容的类型,期望一个字符向量

I'm kinda late to this party but here goes. There is a really simple solution to the error message "Error: incompatible types, expecting a character vector"

只需插入 ungroup()之前的 mutate()函数,你应该可以。

just insert ungroup() prior to your mutate() function and you should be OK.

但我认为它是一个类型在 slice()中的错误。我会提交一个错误报告。

But I think its a bug of some type in slice(). I will file a bug report.

这篇关于对于R的dplyr切片有奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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