dplyr::n() 返回“错误:不应直接调用此函数"; [英] dplyr::n() returns "Error: This function should not be called directly"

查看:11
本文介绍了dplyr::n() 返回“错误:不应直接调用此函数";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做:

dplyr::mutate(MeanValue = mean(RSSI), ReadCount = n())

一切正常.但是当我尝试限定该功能时:

everything works fine. But when I try to qualify the function:

dplyr::mutate(MeanValue = mean(RSSI), ReadCount = dplyr::n())

我收到标题中提到的错误.

I get the error mentioned in the title.

所以,我真的没有问题,我可以避免这样做,但我很好奇它为什么会发生.我已经看过另一个问题(dplyr:n() 中的错误:不应直接调用函数"),但据我所知,dplyr 是我使用的唯一库.无论如何,我尝试按照答案的建议进行操作,但是

So, I do not really have a problem, I can just avoid doing that, but I'm curious about why it even happens. I already looked at another question (dplyr: "Error in n(): function should not be called directly"), but as far as I know, dplyr is the only library I'm using. I tried doing what the answer suggests anyway, but

detach(package:plyr)

结果

分离错误(包:plyr):无效的名称"参数和

Error in detach(package:plyr) : invalid 'name' argument and

conflicts()

没有提到n():

[1] "filter" "lag" "body<-" "intersect" "kronecker" "setdiff" "setequal" "union"
,其中大部分是由 dplyr 引起的.

[1] "filter" "lag" "body<-" "intersect" "kronecker" "setdiff" "setequal" "union"
, most of which is cause by dplyr.

我想我不是唯一对此感到困惑的人吗?

I guess I'm not the only one confused by this?

推荐答案

所以,我真的没有问题,我可以避免[编写dplyr::n()],但我很好奇它为什么会发生.

So, I do not really have a problem, I can just avoid [writing dplyr::n()], but I'm curious about why it even happens.

这是 dplyr 0.5.0 中 dplyr::n 的源代码:

Here's the source code for dplyr::n in dplyr 0.5.0:

function () {
    stop("This function should not be called directly")
}

这就是完全限定形式引发此错误的原因:该函数总是返回错误.(我的猜测是错误抛出函数 dplyr::n 存在,因此 n() 可以有一个带有示例的典型文档页面.)

That's why the fully qualified form raises this error: the function always returns an error. (My guess is that the error-throwing function dplyr::n exists so that n() could have a typical documentation page with examples.)

filter/mutate/summarise 语句中,n() 没有调用这个函数.相反,一些内部函数计算表达式 n() 的组大小.这就是为什么当 dplyr 未加载时以下工作的原因:

Inside of filter/mutate/summarise statements, n() is not calling this function. Instead, some internal function calculates the group sizes for the expression n(). That's why the following works when dplyr is not loaded:

n()
#> Error: could not find function "n"

library(magrittr)
iris %>% 
  dplyr::group_by(Species) %>% 
  dplyr::summarise(n = n())
#> # A tibble: 3 × 2
#>      Species     n
#>       <fctr> <int>
#> 1     setosa    50
#> 2 versicolor    50
#> 3  virginica    50

这里 n() 不能映射到函数,所以我们得到一个错误.但是当在 dplyr 动词内部使用它时,n() 确实映射到某些东西并返回组大小.

Here n() cannot be mapped to a function, so we get an error. But when used it inside of a dplyr verb, n() does map to something and returns group sizes.

这篇关于dplyr::n() 返回“错误:不应直接调用此函数";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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