/
dive-into-dplyr.R
193 lines (143 loc) · 3.5 KB
/
dive-into-dplyr.R
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Dive into dplyr workshop scipt
#Load libraries
library(dplyr)
#Explore iris
head(iris)
pairs(iris)
str(iris)
summary(iris)
#Ex1 Select
select(iris, 1:3)
select(iris, Petal.Width, Petal.Length)
select(iris, contains("Sepal"))
select(iris, starts_with("Sepal"))
select(iris, -Species)
#Ex2 Arrange
arrange(iris, Petal.Width)
arrange(iris, desc(Petal.Width))
arrange(iris, Petal.Width, Petal.Length)
#Ex3 Filter
filter(iris, Petal.Width > 1)
filter(iris, Petal.Width > 1 & Species == "versicolor")
filter(iris, Petal.Width > 1, Species == "versicolor") #the comma is a shorthand for &
filter(iris, !Species == "setosa")
# Magrittr Example (Rene Magritte This is not a pipe)
# Ways I would have done before- nesting or multiple variables
data1 <- filter(iris, Petal.Width > 1)
data2 <- select(data1, Species, Petal.Length)
select(
filter(iris, Petal.Width > 1),
Species, Petal.Length)
#using magrittr to pipe in the data variable
iris %>%
filter(Petal.Width > 1) %>%
select(Species, Petal.Length)
#using the . to specify where the incoming variable will be piped to
iris %>%
myFunction(arg1, arg2 = .)
iris %>%
filter( ., Species == "setosa")
#Ex4 magrittr
# shortcut to make the %>%
# ctl shift m
# shortcut to make the <-
#alt -
iris %>%
filter(Petal.Width > 1) %>%
select(1:3)
iris %>%
select(contains("Petal")) %>%
arrange(Petal.Width) %>%
head()
iris %>%
filter(Species == "setosa") %>%
arrange(desc(Sepal.Width))
iris %>%
filter(Petal.Width > 1) %>%
View()
iris %>%
filter(Species == "setosa") %>%
select(Petal.Width) %>%
unique()
# a second way to get the unique values
iris %>%
filter(Species == "setosa") %>%
distinct(Petal.Width)
#Ex5 Mutate
iris %>%
mutate(pwGreaterThanPL = Petal.Width > Petal.Length) %>%
head()
iris %>%
mutate(pwPlusSL = Petal.Width + Sepal.Length) %>%
head()
iris %>%
mutate(meanSL = mean(Sepal.Length, na.rm = TRUE),
greaterThanMeanSL = ifelse(Sepal.Length > meanSL, 1, 0)) %>%
head()
iris %>%
mutate(slBuckets = cut(Sepal.Length, 3)) %>%
head()
iris %>%
mutate(pwBuckets = case_when(Petal.Width < 0.2 ~ "Low",
Petal.Width >= 0.2 & Petal.Width < 0.6 ~ "Med",
Petal.Width >= 0.6 ~ "High")) %>%
head()
#Ex6 Group_by and Summarise
iris %>%
summarise(plMean = mean(Petal.Length),
pwSD = sd(Petal.Width))
iris %>%
group_by(Species) %>%
mutate(slMean = mean(Sepal.Length))
iris %>%
group_by(Species) %>%
summarise(slMean = mean(Sepal.Length))
iris %>%
group_by(Species, Petal.Length) %>%
summarise(count = n())
#Print and save
(test <- iris %>%
group_by(Species) %>%
summarise(slMean = mean(Sepal.Length)))
#Rename
iris %>%
rename(PL = Petal.Length) %>%
head()
#Slice
iris %>%
slice(2:7)
#ungroup
iris %>%
group_by(Species, Petal.Length) %>%
summarise(count = n()) %>%
ungroup()
#Scoped verbs
#Summarise_all
iris %>%
select(1:4) %>%
summarise_all(mean)
iris %>%
select(1:4) %>%
summarise_all(funs(mean, min))
iris %>%
summarise_all(~length(unique(.)))
#summarise_at
iris %>%
summarise_at(vars(-Species), mean)
iris %>%
summarise_at(vars(contains("Petal")), funs(mean, min))
#summarise_if
iris %>%
summarise_if(is.numeric, sd)
iris %>%
summarise_if(is.factor, ~length(unique(.)))
#other verbs
iris %>%
mutate_if(is.factor, as.character) %>%
str()
iris %>%
mutate_at(vars(contains("Petal")), ~ round(.))
airquality %>%
filter_all(any_vars(is.na(.)))
airquality %>%
filter_all(all_vars(is.na(.)))