I am getting into the programming language R, and I would like to know the difference between dplyr::group_by() or just group_by()?, what does this operator "::" do?.
Thanks!
CodePudding user response:
The first would be appropriate when you were uncertain that the dplyr package were loaded. The two would be equivalent if it were loaded.
CodePudding user response:
In dplyr::group_by(...)
, the ::
operator says to load the dplyr
package if necessary, and to select the group_by
function from that package.
Without the dplyr::
prefix, it just says to find a function named group_by
in the current environment or its ancestor environments.
Using ::
is a bit safer (in a long script or package you might have created a local function named group_by
and you didn't mean to run that one), but also a bit slower.
Once you're writing R packages (which is easier than you think), you can import group_by
explicitly from dplyr
, and get both advantages: safety and speed. It's kind of equivalent to doing
group_by <- dplyr::group_by
... many uses of group_by here ...
where you only pay the cost of ::
once.