準(zhǔn)備工作
library(magrittr)
為什么要使用管道進(jìn)行操作
在書中大神寫了一首詩,我感覺我沒有這個(gè)功力,所以思考了下應(yīng)該如何進(jìn)行。
個(gè)人認(rèn)為應(yīng)用管道操作需要如下幾點(diǎn)
- 變量名稱很多,需要很多的變量名稱去表示
- 代碼非常的長的情況
- 套用函數(shù)非常多
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)
簡單三行代碼需要寫三個(gè)變量名稱,當(dāng)然了變量名稱不應(yīng)該使用abc這種東西。但我僅僅為了展示在簡單的變量名稱下,這樣寫代碼依舊很復(fù)雜。
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這里寫的非常好