dplyr的工作间隔如何? [英] How does dplyr’s between work?
问题描述
我已经阅读了文档,并尝试对其进行谷歌搜索;这应该是一件简单的事情,但似乎对我而言不是;因此,我大胆地问一下,是否有人可以向我解释dplyr的 between()
的工作原理。
I’ve read the documentation and I’ve tried googling it; it should be a simple thing, but it would seem it’s not to me; so I boldly go forth and ask if someone here could explain me how dplyr’s between()
works.
# Explanation documentation
between(x, left, right)
x A numeric vector of values
left, right: Boundary values
我知道向量是一维数组,所以我想 c(1:7)
是向量,对吗?我尝试使用文档中提供的示例作为模板来搜索7月至9月的航班,但是以下内容仅返回错误:
I understand a vector is a one-dimensional array, so I suppose c(1:7)
is a vector, right? I tried using the example provided in the documentation as a template to search for flights july–september, but the following just returns an error:
# Example from documentation cont’d
x <- rnorm(1e2)
x[between(x, -1, 1)]
# Loading the library
library(nycflights13)
# Execute my hopeless attempt at between()
flights[between(month, 7, 9)]
# Output and error message
> flights[between(month, 7, 9)]
Error in between(month, 7, 9) : object 'month' not found
我真是个愚蠢的人,但对此有任何帮助将不胜感激。我也很抱歉没有提出明确的问题;可能会得到赞赏,我真的不知道该怎么说,而是说我不明白。
I feel really daft asking this, but any help in understanding this will be greatly appreciated. I would also apologise for not asking a well-defined question; as is probably appreciated, I really don’t know how to phrase it other than ‘I don’t get it’.
推荐答案
之间
没什么特别的,R中的任何其他函数都会导致相同的问题。您的困惑源于dplyr具有许多功能,这些功能使您可以像对待普通变量一样处理data.frame列名;例如:
between
is nothing special — any other function in R would have led to the same problem. Your confusion stems from the fact that dplyr has a lot of functions that allow you to work on data.frame column names as if they were normal variables; for instance:
filter(flights, month > 9)
但是, 之间
不是这些功能之一。如上所述,这只是正常功能。因此,如果要使用它,则需要以常规方式提供参数。例如:
However, between
is not one of these functions. As mentioned, it’s simply a normal function. So if you want to use it, you need to provide arguments in the conventional way; for instance:
between(flights$month, 7, 9)
这将返回逻辑向量,您现在可以使用它来索引您的数据。
This will return a logical vector, and you can now use it to index your data.frame:
flights[between(flights$month, 7, 9), ]
或更像是dplyr:
flights %>% filter(between(month, 7, 9))
请注意,我们现在在此处使用非标准评估。但是评估是由过滤器
执行的,而不是由之间的$code>执行的。使用标准评估调用
之间的
(通过
过滤器
)。
Note that here we now use non-standard evaluation. But the evaluation is performed by filter
, not by between
. between
is called (by filter
) using standard evaluation.
这篇关于dplyr的工作间隔如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!