The function %%
is the module function, in the sense of modular arithmetic. Note that the result of 17 %% 3
is 2
, for 17 = 3*5 + 2
. Also, see that the result of 17 %/% 3
is 5, complementing the result of %%
. Therefore, the functions %%
and %/%
serve to carry out the entire division within the R.
The function %>%
, also called pipe, serves to chain commands. Perhaps the package that best does this is the dplyr
. Imagine I want to take the dataset iris
and perform some operations on it:
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
Suppose I want to select only columns Petal.Length
and Petal.Width
, whose Sepal.Length
is bigger than 5, but only of setosa species. I can do it in R like this:
subset(iris[iris$Sepal.Length > 5, c(3, 4, 5)], Species=="setosa")
Note how complicated the syntax is. Although they do the job right, the functions subset
and searches for indexes by R
are not very friendly. See how everything gets easier with the pipe:
iris %>%
filter(Sepal.Length > 5) %>%
filter(Species == "setosa") %>%
select(Petal.Length, Petal.Width, Species)
With the pipe I will chain commands. I get the result of the set iris
and pass to the filtration according to the Sepal.Length > 5
; then take this result and step to the filtration Species == "setosa"
; and finally I select only the columns that interest me.
It’s much cleaner to write the codes and much easier to read later, be it your own code written in the past, be it someone else’s code.
Curiosity: the function %>%
appeared in a package called magrittr
, which was previously called plumbr
. The package has test name in honor of René Magritte, Belgian painter who produced the work below:
"This is not a pipe"
The pipe command appeared in another programming language called F#. When it came to R, the package author plumbr
decided to make a big pun, joining the concept of pipe (pipe, in English) with pipe (pipe, in French) and thus honor a famous painter. Source with more details of this story.
Unless you have uploaded some extra package that has left the function
%%
Masked, it is the module function. Note that the result of17 %% 3
is 2, therefore17 = 3*5 + 2
. That is, 2 is the rest of the entire division between 17 and 3. Is your computer reporting something different? As to function%<%
I can’t help you because I only know you%>%
, also called pipe.– Marcus Nunes
I understood, the problem was my concept of module, as being absolute value. <br> I must have written wrong
%>%
.– Márcio Mocellin
Now there are two
%<%
followed in the text and a%any%
in the title. What is this%any%
? Which package can be found in? `sos::findFn('%any%') does not find this function.– Rui Barradas
I believe the AP wants to know what the function means
%>%
(pipe), Rui. At least it was thinking about this that I wrote my reply.– Marcus Nunes
Weird... I was sure I saw reference with
%any%
or%whatever%
, towards anything within the% %
, but now I can’t find the references to it.– Márcio Mocellin