magrittr: A Forward-Pipe Operator for R

Rにパイプ演算子を導入する

> library(magrittr)

バージョン: 1.5


関数名 概略
%$% magrittr exposition pipe-operator
%<>% magrittr compound assignment pipe-operator
%>% magrittr forward-pipe operator
%T>% magrittr tee operator
[[.fseq Extract function(s) from a functional sequence.
debug_fseq Debugging function for functional sequences.
debug_pipe Debugging function for magrittr pipelines.
extract Aliases
freduce Apply a list of functions sequentially
functions Extract the function list from a functional sequence.
magrittr magrittr - Ceci n'est pas un pipe
print.fseq Print method for functional sequence.

%$%

データフレームの変数を参照する

> iris %>%
+   subset(Sepal.Length > mean(Sepal.Length)) %$%
+   cor(Sepal.Length, Sepal.Width)
[1] 0.3361992

%<>%

> x <- rnorm(100)
> x
  [1]  1.53261063 -0.23570036 -1.02642090 -0.71040656  0.25688371
  [6] -0.24669188 -0.34754260 -0.95161857 -0.04502772 -0.78490447
 [11] -1.66794194 -0.38022652  0.91899661 -0.57534696  0.60796432
 [16] -1.61788271 -0.05556197  0.51940720  0.30115336  0.10567619
 [21] -0.64070601 -0.84970435 -1.02412879  0.11764660 -0.94747461
 [26] -0.49055744 -0.25609219  1.84386201 -0.65194990  0.23538657
 [31]  0.07796085 -0.96185663 -0.07130809  1.44455086  0.45150405
 [36]  0.04123292 -0.42249683 -2.05324722  1.13133721 -1.46064007
 [41]  0.73994751  1.90910357 -1.44389316  0.70178434 -0.26219749
 [46] -1.57214416 -1.51466765 -1.60153617 -0.53090652 -1.46175558
 [51]  0.68791677  2.10010894 -1.28703048  0.78773885  0.76904224
 [56]  0.33220258 -1.00837661 -0.11945261 -0.28039534  0.56298953
 [61] -0.37243876  0.97697339 -0.37458086  1.05271147 -1.04917701
 [66] -1.26015524  3.24103993 -0.41685759  0.29822759  0.63656967
 [71] -0.48378063  0.51686204  0.36896453 -0.21538051  0.06529303
 [76] -0.03406725  2.12845190 -0.74133610 -1.09599627  0.03778840
 [81]  0.31048075  0.43652348 -0.45836533 -1.06332613  1.26318518
 [86] -0.34965039 -0.86551286 -0.23627957 -0.19717589  1.10992029
 [91]  0.08473729  0.75405379 -0.49929202  0.21444531 -0.32468591
 [96]  0.09458353 -0.89536336 -1.31080153  1.99721338  0.60070882
> x %<>% abs() %>% sort()
> x
  [1] 0.03406725 0.03778840 0.04123292 0.04502772 0.05556197 0.06529303
  [7] 0.07130809 0.07796085 0.08473729 0.09458353 0.10567619 0.11764660
 [13] 0.11945261 0.19717589 0.21444531 0.21538051 0.23538657 0.23570036
 [19] 0.23627957 0.24669188 0.25609219 0.25688371 0.26219749 0.28039534
 [25] 0.29822759 0.30115336 0.31048075 0.32468591 0.33220258 0.34754260
 [31] 0.34965039 0.36896453 0.37243876 0.37458086 0.38022652 0.41685759
 [37] 0.42249683 0.43652348 0.45150405 0.45836533 0.48378063 0.49055744
 [43] 0.49929202 0.51686204 0.51940720 0.53090652 0.56298953 0.57534696
 [49] 0.60070882 0.60796432 0.63656967 0.64070601 0.65194990 0.68791677
 [55] 0.70178434 0.71040656 0.73994751 0.74133610 0.75405379 0.76904224
 [61] 0.78490447 0.78773885 0.84970435 0.86551286 0.89536336 0.91899661
 [67] 0.94747461 0.95161857 0.96185663 0.97697339 1.00837661 1.02412879
 [73] 1.02642090 1.04917701 1.05271147 1.06332613 1.09599627 1.10992029
 [79] 1.13133721 1.26015524 1.26318518 1.28703048 1.31080153 1.44389316
 [85] 1.44455086 1.46064007 1.46175558 1.51466765 1.53261063 1.57214416
 [91] 1.60153617 1.61788271 1.66794194 1.84386201 1.90910357 1.99721338
 [97] 2.05324722 2.10010894 2.12845190 3.24103993

%T>

> rnorm(200) %>%
+ matrix(ncol = 2) %T>%
+   plot() %>%
+   colSums()