Ref: vignette
library(magrittr)
car_data <-
mtcars %>%
subset(hp > 100) %>% # extract a subset
aggregate(. ~ cyl, data = ., FUN = . %>% mean %>% round(2)) %>% # aggregate info on n.cylinders
transform(kpl = mpg %>% multiply_by(0.4251)) %>% # add new column
print # print result before assign
## cyl mpg disp hp drat wt qsec vs am gear carb kpl
## 1 4 25.90 108.0 111.0 3.94 2.15 17.75 1.00 1.00 4.50 2.00 11.010
## 2 6 19.74 183.3 122.3 3.59 3.12 17.98 0.57 0.43 3.86 3.43 8.391
## 3 8 15.10 353.1 209.2 3.23 4.00 16.77 0.00 0.14 3.29 3.50 6.419
Some notes:
By default the left-hand side (LHS) will be piped in as the first argument of the function appearing on the right-hand side (RHS). When the LHS is needed at a position other than the first, one can use the dot, .
, as placeholder. This is used in the aggregate
expression.
Whenever only one argument is needed, the LHS, then one can omit the empty parentheses (just like in print
).
A pipeline with a dot (.) as LHS will create a unary function. This is used to define the aggregator function.
Other .
egs:
1:9 %>% paste(letters[.])
## [1] "1 a" "2 b" "3 c" "4 d" "5 e" "6 f" "7 g" "8 h" "9 i"
1:9 %>% paste(letters[.], .)
## [1] "a 1" "b 2" "c 3" "d 4" "e 5" "f 6" "g 7" "h 8" "i 9"
1:9 %>% { paste(letters[.]) } # use {} to prevent placing '.' as the 1st function argument
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i"
It’s possible to use %>%
into anonymous functions:
car_data %>%
{
if (nrow(.) > 0)
rbind(head(., 1), tail(., 1))
else .
} %>%
.[,1:3]
## cyl mpg disp
## 1 4 25.9 108.0
## 3 8 15.1 353.1
Whenever you want to use a function- or call-generating statement as right-hand side, parentheses are used to evaluate the right-hand side before piping takes place:
1:10 %>% (substitute(f(), list(f = sum))) # with the outside (), it would be an error
## [1] 55
The “tee” operator, %T>% works like %>%, except it returns the left-hand side value, and not the result of the right-hand side operation. This is useful when a step in a pipeline is used for its side-effect (printing, plotting, logging, etc.).
rnorm(200) %>%
matrix(ncol = 2) %T>%
plot %>% # plot usually does not return anything.
colSums
## [1] 11.702 -2.394
The “exposition” pipe operator, %$% exposes the names within the left-hand side object to the right-hand side expression. Essentially, it is a short-hand for using the with functions (and the same left-hand side objects are accepted).
iris %>%
subset(Sepal.Length > mean(Sepal.Length)) %$%
cor(Sepal.Length, Sepal.Width)
## [1] 0.3362
data.frame(z = rnorm(100)) %$%
ts.plot(z)
The compound assignment pipe operator %<>%
can be used as the first pipe in a chain. The effect will be that the result of the pipeline is assigned to the left-hand side object, rather than returning the result as usual. It is essentially shorthand notation for expressions like foo <- foo %>% bar %>% baz
, which boils down to foo %<>% bar %>% baz
.
x <- 1:9
x %<>% sqrt %>% add(1)
x
## [1] 2.000 2.414 2.732 3.000 3.236 3.449 3.646 3.828 4.000
Function add(1)
is one of several aliases the package has to help writing expressions. Check the helpfile for more egs.