-
Notifications
You must be signed in to change notification settings - Fork 3
/
moreWithPipes.Rmd
76 lines (45 loc) · 1.55 KB
/
moreWithPipes.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
## More with pipes
Recap thus far:
<span class='emph'>%>%</span> : Passes the prior object to the function after the pipe
- x <span class='emph'>%>%</span> f same as f(x)
Example:
```{r `%>%`}
iris %>% head
head(iris)
```
## More with pipes
Other pipes are available with <span class="pack">magrittr</span> and other packages
<span class='emph'>%$%</span>: Exposes the names in the prior to the function after
- x <span class='emph'>%\$%</span> y(a, b) same as y(x\$a, x\$b)
Example (no data argument needed for <span class="func">lm</span>):
```{r `%$%`}
iris %$% lm(Sepal.Length ~ Sepal.Width)
```
## More with pipes
<span class='emph'>%T>%</span> : Passes to the next ***and*** subsequent steps
- x <span class='emph'>%T>%</span> y <span class='emph'>%>%</span> z is the same as x <span class='emph'>%>%</span> y & x <span class='emph'>%>%</span> z
## More with pipes
Example:
```{r Tpipe, fig.width=3, fig.height=3, echo=-1}
# par(mai=c(.1,.1,.1,.1), pch=19, byt='n')
iris %>% select(Sepal.Length, Sepal.Width) %T>% plot %>% summary
```
## More with pipes
Unfortunately the T pipe does not allow for some things
- Works:
```{r Tprobs1, eval=FALSE}
iris %>% select(Sepal.Length, Sepal.Width) %T>% plot %>% summary
```
- Provides no summary:
```{r Tprobs2, eval=FALSE}
iris %>% select(Sepal.Length, Sepal.Width) %T>% summary %>% plot
```
- Somewhat limiting in my opinion.
## More with pipes
<span class='emph'>%<>%</span> : assigns to former object the operations that follow
- Example:
```{r `%<>%`}
x = c(1:3,9:7,10,4:6)
x %<>% .^2 %>% sort
x
```