magrittr: pipe
lhs %>% rhs
forward-pipe
- lhs为rhs第一个参数时:
x %>% f(y)等价于 f(x, y)
- lhs在任意位置时,用点(.)代替:
z %>% f(x, y, arg = .)等价于 f(x, y, arg = z)
- rhs为代码块:
rnorm(100) %>% {c(min(.), mean(.), max(.))} %>% floor
lhs %<>% rhs
复合赋值管道运算符
%<>%
用于首先将lhs
传递给rhs
表达式,最后将值重新赋给lhs
。some_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