Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does dplyr::n function work?

Tags:

r

dplyr

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

like image 678
Gere Caste Avatar asked Dec 30 '16 13:12

Gere Caste


People also ask

What does dplyr :: filter do in R?

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

What does dplyr :: Select do in R?

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.

What does %>% do in dplyr?

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

What does Top N do in R?

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.


1 Answers

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.

like image 68
Axeman Avatar answered Sep 28 '22 15:09

Axeman