Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

r - How does dplyr’s between work?

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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), ]

Or, more dplyr-like:

flights %>% filter(between(month, 7, 9))

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...