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

查看:764
本文介绍了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 ):无效'name'参数

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



conflicts()

没有提到n():


[1]filterlagbody - intersectkroneckersetdiffsetequalunion

,其中大部分是由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 :: n in dplyr 0.5.0:

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.)

过滤器 / mutate / 总结语句, 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天全站免登陆