- What does the
%<>%
operator do in R ? - What's the difference between using
%<>%
and<-
? - In what type of circumstances
%<>%
could be useful ?
Asked
Active
Viewed 5,051 times
4

rafa.pereira
- 13,251
- 6
- 71
- 109
-
2It is similar to `:=` in data.table. – akrun Jun 12 '16 at 10:19
-
2@akrun does it update in-place? it is the whole point of `:=` – jangorecki Jun 12 '16 at 11:55
-
Possible duplicate of [What does %>% mean in R](http://stackoverflow.com/questions/24536154/what-does-mean-in-r) – John Paul Jun 13 '16 at 02:25
1 Answers
16
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

Gregor Thomas
- 136,190
- 20
- 167
- 294

lukeA
- 53,097
- 5
- 97
- 100