magrittr: 管道语法

magrittr: pipe

lhs %>% rhs forward-pipe

  1. lhs为rhs第一个参数时:x %>% f(y)等价于 f(x, y)
  2. lhs在任意位置时,用点(.)代替:z %>% f(x, y, arg = .)等价于 f(x, y, arg = z)
  3. rhs为代码块:rnorm(100) %>% {c(min(.), mean(.), max(.))} %>% floor

lhs %<>% rhs复合赋值管道运算符

%<>%用于首先将lhs传递给rhs表达式,最后将值重新赋给lhssome_object %<>% foo %>% bar
相当于some_object <- some_object %>% foo %>% bar

lhs %$% rhs

iris %>%
subset(Sepal.Length > mean(Sepal.Length)) %$%
cor(Sepal.Length, Sepal.Width)

lhs %T>% rhs T运算符
返回lhs本身,而不是rhs函数或表达式,对于print或者plot类似函数非常有用。

rnorm(200)%>%
matrix(ncol = 2)%T>%
plot %>% #plot通常不返回任何内容。
colSums