I am curious about how the function n
from dplyr
is programmed. When evaluating n
in the dplyr
env, all I get is this:
function ()
{
stop("This function should not be called directly")
}
<environment: namespace:dplyr>
Maybe it's a silly question but, where is it defined? How come it works when called inside some other functions? In which env is it hidden?
Thanks for your help
The filter() function is used to subset a data frame, retaining all rows that satisfy your conditions. To be retained, the row must produce a value of TRUE for all conditions. Note that when a condition evaluates to NA the row will be dropped, unlike base subsetting with [ .
select() is a function from dplyr R package that is used to select data frame variables by name, by index, and also is used to rename variables while selecting, and dropping variables by name.
%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).
If n is positive, selects the top rows. If negative, selects the bottom rows. If x is grouped, this is the number (or fraction) of rows per group.
As far as I understand, dplyr
uses hybrid evaluation. That means it will evaluate some parts of the expression in C++ and others in R. n()
is one of the functions that always gets handled by C++. This is why the function doesn't do anything in R
directly, except for returning an error, since the function is never evaluated by R.
The relevant C++
code can be found on github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With