第13章使用magrittr进行管道操作

准备工作

library(magrittr)

为什么要使用管道进行操作

在书中大神写了一首诗,我感觉我没有这个功力,所以思考了下应该如何进行。
个人认为应用管道操作需要如下几点

  1. 变量名称很多,需要很多的变量名称去表示
  2. 代码非常的长的情况
  3. 套用函数非常多
    the magrittr package offers a set of operators which promote semantics that will improve your code by structuring sequences of data operations left-to-right (as opposed to from the inside and out),avoiding nested function calls,minimizing the need for local variables and function definitions, and making it easy to add steps anywhere in the sequence of operations.
a<- 1+1
b<- seq(a+1)
c<View(b)

简单三行代码需要写三个变量名称,当然了变量名称不应该使用abc这种东西。但我仅仅为了展示在简单的变量名称下,这样写代码依旧很复杂。

a<-1+1 %>%seq(.+1)%>% View()

这样你很容易理解了这行代码具体是做了什么。
除了简单的%>%还有一些其他的管道操作

%>% forward-pipe operator.
%T>% tee operator.
%<>% compound assignment pipe-operator. (大神不建议这样做,要听话)
%$% exposition pipe-operator.

https://blog.csdn.net/fairewell/article/details/72878107这里写的非常好

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容