What does %% function mean in R?
%...% operators
%>%
has no builtin meaning but the user (or a package) is free to define operators of the form %whatever%
in any way they like. For example, this function will return a string consisting of its left argument followed by a comma and space and then it's right argument.
"%,%" <- function(x, y) paste0(x, ", ", y)
# test run
"Hello" %,% "World"
## [1] "Hello, World"
The base of R provides %*%
(matrix mulitiplication), %/%
(integer division), %in%
(is lhs a component of the rhs?), %o%
(outer product) and %x%
(kronecker product). It is not clear whether %%
falls in this category or not but it represents modulo.
expm The R package, expm, defines a matrix power operator %^%
. For an example see Matrix power in R .
operators The operators R package has defined a large number of such operators such as %!in%
(for not %in%
). See http://cran.r-project.org/web/packages/operators/operators.pdf
igraph This package defines %--% , %->% and %<-% to select edges.
lubridate This package defines %m+% and %m-% to add and subtract months and %--% to define an interval. igraph also defines %--% .
Pipes
magrittr In the case of %>%
the magrittr R package has defined it as discussed in the magrittr vignette. See http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html
magittr has also defined a number of other such operators too. See the Additional Pipe Operators section of the prior link which discusses %T>%
, %<>%
and %$%
and http://cran.r-project.org/web/packages/magrittr/magrittr.pdf for even more details.
dplyr The dplyr R package used to define a %.%
operator which is similar; however, it has been deprecated and dplyr now recommends that users use %>%
which dplyr imports from magrittr and makes available to the dplyr user. As David Arenburg has mentioned in the comments this SO question discusses the differences between it and magrittr's %>%
: Differences between %.% (dplyr) and %>% (magrittr)
pipeR The R package, pipeR, defines a %>>%
operator that is similar to magrittr's %>% and can be used as an alternative to it. See http://renkun.me/pipeR-tutorial/
The pipeR package also has defined a number of other such operators too. See: http://cran.r-project.org/web/packages/pipeR/pipeR.pdf
postlogic The postlogic package defined %if%
and %unless%
operators.
wrapr The R package, wrapr, defines a dot pipe %.>%
that is an explicit version of %>%
in that it does not do implicit insertion of arguments but only substitutes explicit uses of dot on the right hand side. This can be considered as another alternative to %>%
. See https://winvector.github.io/wrapr/articles/dot_pipe.html
Bizarro pipe. This is not really a pipe but rather some clever base syntax to work in a way similar to pipes without actually using pipes. It is discussed in http://www.win-vector.com/blog/2017/01/using-the-bizarro-pipe-to-debug-magrittr-pipelines-in-r/ The idea is that instead of writing:
1:8 %>% sum %>% sqrt
## [1] 6
one writes the following. In this case we explicitly use dot rather than eliding the dot argument and end each component of the pipeline with an assignment to the variable whose name is dot (.
) . We follow that with a semicolon.
1:8 ->.; sum(.) ->.; sqrt(.)
## [1] 6
Update Added info on expm package and simplified example at top. Added postlogic package.
Update 2 The development version of R has defined a |>
pipe. Unlike magrittr's %>%
it can only substitute into the first argument of the right hand side. Although limited, it works via syntax transformation so it has no performance impact.
What does %% mean in R
The infix operator %>%
is not part of base R, but is in fact defined by the package magrittr
(CRAN) and is heavily used by dplyr
(CRAN).
It works like a pipe, hence the reference to Magritte's famous painting The Treachery of Images.
What the function does is to pass the left hand side of the operator to the first argument of the right hand side of the operator. In the following example, the data frame iris
gets passed to head()
:
library(magrittr)
iris %>% head()
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
Thus, iris %>% head()
is equivalent to head(iris)
.
Often, %>%
is called multiple times to "chain" functions together, which accomplishes the same result as nesting. For example in the chain below, iris
is passed to head()
, then the result of that is passed to summary()
.
iris %>% head() %>% summary()
Thus iris %>% head() %>% summary()
is equivalent to summary(head(iris))
. Some people prefer chaining to nesting because the functions applied can be read from left to right rather than from inside out.
Error: could not find function %%
You need to load a package (like magrittr
or dplyr
) that defines the function first, then it should work.
install.packages("magrittr") # package installations are only needed the first time you use it
install.packages("dplyr") # alternative installation of the %>%
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr) # alternatively, this also loads %>%
The pipe operator %>%
was introduced to "decrease development time and to improve readability and maintainability of code."
But everybody has to decide for himself if it really fits his workflow and makes things easier.
For more information on magrittr
, click here.
Not using the pipe %>%
, this code would return the same as your code:
words <- colnames(as.matrix(dtm))
words <- words[nchar(words) < 20]
words
EDIT:
(I am extending my answer due to a very useful comment that was made by @Molx)
Despite being from
magrittr
, the pipe operator is more commonly used
with the packagedplyr
(which requires and loadsmagrittr
), so
whenever you see someone using%>%
make sure you shouldn't loaddplyr
instead.
Meaning of Symbol %% in R
%>%
means whatever you want it to mean, in Base R anyway:
> %>%
Error: unexpected SPECIAL in "%>%"
(which means that symbol is not defined.)
Binary operators are ones that have an input from the left and from the right of the operator, just like *
, +
etc. You use them as you would mathematically like a * b
, which R turns into the call '*'(a, b)
. R allows you to add your own binary operators via the %foo%
syntax, with foo
replace by whatever you want, as long as it hasn't already been used by R, which includes %*%
and %/%
for example.
`%foo%` <- function(x, y) paste("foo", x, "and foo", y)
> 1 %foo% 2
[1] "foo 1 and foo 2"
%>%
takes on a specific and well-defined meaning once you load the magrittr R package for example, where it is used as a pipe operator might be in a Unix shell to chain together a series of function calls.
What does %*% mean in R
Use ?'%*%'
to get the documentation.
%*%
is matrix multiplication. For matrix multiplication, you need an m x n
matrix times an n x p
matrix.
What does the %% operator mean in R?
The help, ?magrittr::`%<>%`
, answers all your questions, if you are refering to magrittr`s compound assignment pipe-operator:
[...]
%<>%
is used to update a value
by first piping it into one or more rhs expressions, and then
assigning the result. For example,some_object %<>% foo %>% bar
is
equivalent tosome_object <- some_object %>% foo %>% bar
. It must be
the first pipe-operator in a chain, but otherwise it works like%>%
.
So
library(magrittr)
set.seed(1);x <- rnorm(5)
x %<>% abs %>% sort
x
# [1] 0.1836433 0.3295078 0.6264538 0.8356286 1.5952808
is the same as
set.seed(1);x <- rnorm(5)
x <- sort(abs(x))
x
# [1] 0.1836433 0.3295078 0.6264538 0.8356286 1.5952808
What do %/% and %% mean?
From the ?"%%"
help page
%%
indicates x mod y and%/%
indicates integer division. It is guaranteed thatx == (x %% y) + y * ( x %/% y )
(up to rounding error) unless y == 0 where the result of%%
is NA_integer_ or NaN (depending on the typeof of the arguments).
How do I get mean functions to work when I use piping?
In dplyr
, you can use summarise()
whenever you're not changing your original dataframe (reordering it, filtering it, adding to it, etc), but instead are creating a new dataframe that has summary statistics for the first dataframe.
mtcars %>%
summarise(mean_mpg = mean(mpg))
gives the output:
mean_mpg
1 20.09062
PS. If you're learning dplyr
, learning these five verbs will take you a long way: select()
, filter()
, group_by()
, summarise()
, arrange()
.
Related Topics
Extract Regression Coefficient Values
Ggplot Legends - Change Labels, Order and Title
Aggregate a Dataframe on a Given Column and Display Another Column
Nested Facets in Ggplot2 Spanning Groups
Dplyr: How to Use Group_By Inside a Function
Converting Decimal to Binary in R
Remove Columns With Zero Values from a Dataframe
How to Display the Frequency At the Top of Each Factor in a Barplot in R
Alternate, Interweave or Interlace Two Vectors
Cumulatively Paste (Concatenate) Values Grouped by Another Variable
R.Exe, Rcmd.Exe, Rscript.Exe and Rterm.Exe: What's the Difference
R Apply() Function on Specific Dataframe Columns
How to Make Execution Pause, Sleep, Wait For X Seconds in R
Merge Several Data.Frames into One Data.Frame With a Loop
Ggplot2: Histogram With Normal Curve
Use Variable Names in Functions of Dplyr
How to Assign from a Function Which Returns More Than One Value