-
Notifications
You must be signed in to change notification settings - Fork 3
/
data-manipulation-scug.Rpres
322 lines (258 loc) · 7.32 KB
/
data-manipulation-scug.Rpres
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<style type="text/css">
.small-code pre code {
font-size: 0.9em;
}
</style>
Data Manipulation in R & Python: the dplyr, data.table, and dplython packages
========================================================
<span style = "color:darkred"> _Som B. Bohora_ </span>
[Department of Pediatrics](http://www.oumedicine.com/pediatrics)
University of Oklahoma Health Sciences Center
October 04, 2016
Outline of the presentation
========================================================
1. **`dplyr`** in R
1. **`dplython`** in python
1. **`data.table`** in R
why `dplyr`?
========================================================
1. Speed and performance
1. Direct connection to and analysis within external databases
1. Function chaining
1. Syntax simplicity and ease of use (SQL flavor)
`dplyr` verbs
========================================================
6 key operations
* **filter**: filter the rows of a data frame
* **mutate**: modify or create new columns
* **group by**: set grouping variables
* **summarise**: aggregate a data frame
* **arrange**: sort columns of a data frame
* **select**: select a set of columns
filter
========================================================
```{r}
ds <- iris[,3:5]
dplyr::filter(ds, Species == "setosa", Petal.Width > 0.2)
```
mutate (transmute??)
========================================================
```{r, echo=TRUE}
dplyr::mutate(ds, ratio_length_width = Petal.Length / Petal.Width,
sqrt_width = sqrt(Petal.Width))
```
mutate (referring to newly created columns)
========================================================
```{r, echo=TRUE}
dplyr::mutate(ds, ratio_length_width = Petal.Length / Petal.Width,
sqrt_width = sqrt(Petal.Width),
sq_width = sqrt_width^2)
```
group_by
========================================================
```{r, echo=TRUE}
grouped_ds <- dplyr::group_by(mtcars[,9:11], am, gear)
grouped_ds
```
summarize
========================================================
```{r, echo=TRUE}
dplyr::summarize(mtcars, mean_mpg = mean(mpg))
```
arrange
========================================================
```{r, echo=TRUE}
dplyr::arrange(mtcars[,1:3], desc(mpg), cyl)
```
select
========================================================
```{r, echo=TRUE}
library(dplyr)
head(select(mtcars, 1:3),1)
head(select(mtcars, mpg, cyl, disp),1)
head(select(mtcars, mpg:disp),1)
```
`select` continued
========================================================
```{r, echo=TRUE}
library(dplyr)
head(select(mtcars, contains('r')),2)
head(select(mtcars, starts_with('d')),2)
```
Putting pieces together
========================================================
```{r, echo=TRUE}
library(dplyr)
df <- mutate(mtcars, disp_cyl = disp/cyl)
df <- filter(df, disp_cyl > 30, mpg < 25)
df <- group_by(df, cyl, gear)
df
```
Putting pieces together continued
========================================================
```{r}
summarise(
group_by(
filter(
mutate(mtcars, disp_cyl = disp/cyl),
disp_cyl > 30,
mpg > 23
),
cyl,
gear
),
avg_d_cyl = mean(disp_cyl),
min_d_cyl = min(disp_cyl)
)
```
Using `%>%` from `magrittr`
====================================
Without pipes
```r
summarise(
group_by(
filter(
mutate(mtcars, disp_cyl = disp/cyl),
disp_cyl > 30,
mpg > 23
),
cyl,
gear
),
avg_d_cyl = mean(disp_cyl),
min_d_cyl = min(disp_cyl)
)
```
***
With pipes
```r
mtcars %>%
mutate(disp_cyl = disp/cyl) %>%
filter(disp_cyl>30, mpg>23) %>%
group_by(cyl, gear) %>%
summarise(
avg_d_cyl = mean(disp_cyl),
min_d_cyl = min(disp_cyl)
)
```
Adding `ggplot2`
====================================
```{r fig.align='center'}
library(magrittr); library(ggplot2)
mtcars %>%
dplyr::select(mpg, disp) %>%
dplyr::arrange(mpg, disp) %>%
ggplot(aes(x= mpg, y = disp)) +
geom_point(size = 4, col = "blue") + theme_bw()
```
`dplyr` and `dplython` functions
====================================
| `dplyr` | `dplython` |
|:-----------|------------:|
| `filter` | `sift` |
| `mutate` | same |
| `group_by` | same |
| `summarize`| same |
| `arrange` | same |
| `select` | same |
`dplython` demonstration
====================================
`data.table` package
====================================
* Extension of `data.drame`
* Fast aggregation of large data
* Fast ordered joins
* Fast add/modify/delete of columns by group, and a fast file reader (fread)
* Natural and flexible syntax
`data.table` package continued
====================================
<img src="data.table.png" alt ="logo" height = "600" width = "1000" align ="right"/>
`data.table` package (row operations)
====================================
```{r, echo=FALSE}
library(data.table)
DF <- data.frame(V1=c(1L,2L),
V2=LETTERS[1:3],
V3=round(rnorm(100000),4),
V4=c(1:300000))
DT <- as.data.table(DF)
```
```{r, eval=FALSE}
DT[3:5,]
DT[3:5]
DT[V2 == "A"]
DT[ V2 %in% c("A", "C")]
```
`data.table` package (column operations)
====================================
```{r eval=FALSE}
DT[, V2]
DT["V2"] # would NOT select V2 column. We need DT[,"V2"] or DT[,V2]
DT[, .(V2, V3)] # .() is an alias to list() to result data.table object
DT[, sum(V4)]
DT[, .(sum(V1), sd(V3))]
DT[, .(avg = sum(V1), std = sd(V3))] # Assign column names
DT[, .(V1, std = sd(V3))] # columns reclycled if different lengths
DT[, {print(V2) # multiple expressions
plot(V3)
NULL}]
```
`data.table` (group by)
====================================
```{r, echo=FALSE}
DF <- data.frame(V1=c(1L,2L),
V2=LETTERS[1:3],
V3=round(rnorm(4),4),
V4=c(1:12))
DT <- as.data.table(DF)
```
```{r, eval=FALSE}
DT[, .(sum.V4 = sum(V4)), by=V1]
DT[, .(sum.V4 = sum(V4)), by=.(V1,V2)]
DT[1:5, .(sum.V4 = sum(V4)), by=.(V1,V2)] # only for subset rows
DT[, .N, by = V1] # total no. of observations
# DT[V1 ==1, sum(V4)]
```
`data.table` (adding/updating columns, :=)
====================================
```{r, eval=FALSE}
DT[, V1 := round(exp(V1),2)] # update V1
DT[, c("V1","V2") := list (round(exp(V1),2), LETTERS[4:6])] # update multiple columns
DT[, V1 := NULL] # remove column
DT[, c("V4","V3") := NULL] # remove multiple columns
```
`data.table` (setkey)
====================================
```{r, eval=FALSE}
setkey(DT,V2) # sets the key
DT["A"]
DT[c("A","C")]
DT[c("A","C"), sum(V4)]
setkey(DT, NULL) # removes the key
```
`data.table` (advanced operations)
====================================
```{r eval=FALSE}
DT[.N-2] # second row
DT[, .N] # no. of rows
DT[,.(V2,V3)] # or
DT[,list(V2,V3)]
DT[,mean(V3), by= .(V1,V2)]
```
`data.table` (advanced operations cont.)
====================================
```{r, eval=FALSE}
#.SD
DT[,.SD[c(1,.N)], by=V2]
DT[, lapply(.SD, sum), by=V2]
DT[, lapply(.SD,sum), by=V2,.SDcols = c("V3","V4")] #same, but only for V3,V4
DT[, lapply(.SD, function(x) sum(x, na.rm = TRUE)), by = V2]
DT[, .(V4.Sum = sum(V4)), by=V1][V4.Sum > 25 ] # SQL like having
```
<!-- References -->
<!-- ==================================== -->
<!-- dplyr: (https://github.com/hadley/dplyr/) -->
<!-- dplython: (https://github.com/dodger487/dplython/) -->
<!-- data.table: (https://s3.amazonaws.com/assets.datacamp.com/img/blog/) -->
Questions?
====================================