-
Notifications
You must be signed in to change notification settings - Fork 0
/
week9-data-wrangling.R
321 lines (207 loc) · 9.36 KB
/
week9-data-wrangling.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
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
## ----setup, include=FALSE---------------------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
## ----message=FALSE, warning=FALSE-------------------------------------------------------
library(dplyr)
library(tidyr)
library(ggplot2)
## ---------------------------------------------------------------------------------------
1:10 %>% sqrt() %>% mean() %>% round(digits=1)
## ---------------------------------------------------------------------------------------
round(mean(sqrt(1:10)),digits=1)
## ---------------------------------------------------------------------------------------
fun <- function(a=1, b=10){c(max(a), min(b))}
a <- 2:10
a %>% fun(a^2)
## ---------------------------------------------------------------------------------------
a <- 3
a %>% rnorm(n=a)
a %>% rnorm()
## ----message=FALSE----------------------------------------------------------------------
library(microbenchmark)
bm <- microbenchmark(1:10 %>% sqrt() %>% mean() %>% round(digits=1), round(mean(sqrt(1:10)),digits=1) )
ggplot2::autoplot(bm)
## ---------------------------------------------------------------------------------------
fun <- function(x, cyl=1){
x %>% transform(new = mpg^cyl) %>% select(mpg, new)
}
## ---------------------------------------------------------------------------------------
df <- data.frame(mpg=runif(10, 10, 30))
fun(df, cyl=0.5)
## ---------------------------------------------------------------------------------------
fun(mtcars[1:5,], cyl=0.5)
## ---------------------------------------------------------------------------------------
head(mtcars)
## ---------------------------------------------------------------------------------------
mtcars %>% select(mpg)
## ---------------------------------------------------------------------------------------
mtcars %>% select(-mpg, -qsec, -gear, -cyl)
## ---------------------------------------------------------------------------------------
mtcars %>% select(disp:qsec)
## ---------------------------------------------------------------------------------------
mtcars %>% select(!disp:qsec)
## ---------------------------------------------------------------------------------------
df %>% select(where(is.numeric))
## ---------------------------------------------------------------------------------------
mtcars %>% filter(cyl==4)
## ---------------------------------------------------------------------------------------
mtcars %>% filter(cyl==4, hp>90)
## ---------------------------------------------------------------------------------------
mtcars %>%
summarize(mean.wt=mean(wt))
## ---------------------------------------------------------------------------------------
mtcars %>%
summarize(mean.wt=mean(wt)) %>%
round(digits=2)
## ---------------------------------------------------------------------------------------
round(mean(mtcars$wt), digits=2)
## ---------------------------------------------------------------------------------------
mtcars %>%
summarize(mean.wt=mean(c(mpg, hp)))
## ---------------------------------------------------------------------------------------
mean(c(mtcars$mpg, mtcars$hp))
## ---------------------------------------------------------------------------------------
mtcars %>%
summarize(mean.wt=mean(mpg:hp))
## ---------------------------------------------------------------------------------------
mtcars[1,"mpg"]:mtcars[1,"hp"]
## ---------------------------------------------------------------------------------------
mean(mtcars[1,"mpg"]:mtcars[1,"hp"])
## ---------------------------------------------------------------------------------------
mtcars %>% summarize_at(vars(mpg:hp), mean)
## ---------------------------------------------------------------------------------------
mtcars %>%
select(mpg:hp) %>%
summarize_all(mean)
## ---------------------------------------------------------------------------------------
apply(mtcars[,1:4],2,mean)
## ---------------------------------------------------------------------------------------
df <- mtcars[,1:3]; df$label="test"
df %>% summarize_all(mean)
## ---------------------------------------------------------------------------------------
apply(df,2,mean)
## ---------------------------------------------------------------------------------------
mtcars %>%
filter(cyl==4) %>%
select(mpg:hp) %>%
summarize_all(mean) %>%
round(digits=2) %>%
paste(collapse=" -- ")
## ---------------------------------------------------------------------------------------
df2 <- data.frame(id=paste0(rep("s",8),1:2),
pop=paste0(rep("r",8),rep(1:2,each=4)),
Week1=rnorm(8), Week2=rnorm(8), Week3=rnorm(8), Week4=rnorm(8))
head(df2)
## ---------------------------------------------------------------------------------------
df2 %>% group_by(pop) %>% summarize_all(mean)
## ---------------------------------------------------------------------------------------
apply(df2[df2$pop=="r1",3:6],2,mean)
## ---------------------------------------------------------------------------------------
df2 %>%
group_by(pop, id) %>%
summarize(mean=mean(c(Week1, Week2, Week3, Week4)))
## ---------------------------------------------------------------------------------------
df2 %>% group_by(pop) %>%
select(where(is.numeric)) %>%
summarize_all(mean)
## ---------------------------------------------------------------------------------------
df2 %>% group_by(pop) %>%
select(ends_with("2")) %>%
summarize_all(mean)
## ---------------------------------------------------------------------------------------
mtcars %>%
select(mpg:wt) %>%
mutate(wt.hp=wt/hp)
## ---------------------------------------------------------------------------------------
fun <- function(x,y){fit <- lm(y~x); coef(fit)[1]} #return interept
mtcars %>%
group_by(carb) %>%
summarize(lmm=fun(mpg,hp))
## ---------------------------------------------------------------------------------------
mtcars %>%
group_by(carb) %>%
mutate(lmm=fun(mpg,hp)) %>%
select(mpg, hp, lmm)
## ---------------------------------------------------------------------------------------
mtcars %>%
select(mpg:wt) %>%
mutate(mean=sqrt(wt))
## ---------------------------------------------------------------------------------------
mtcars %>%
select(mpg:wt) %>%
mutate(mean=mean(wt))
## ---------------------------------------------------------------------------------------
mtcars %>%
select(mpg:wt) %>%
mutate(mean1 = (wt+hp)/2, mean2=mean(c(wt,hp)))
## ----message=FALSE----------------------------------------------------------------------
bm <- microbenchmark(
apply(mtcars,2,sum),
summarize_all(mtcars, sum)
)
ggplot2::autoplot(bm)
## ----message=FALSE----------------------------------------------------------------------
bm <- microbenchmark(
mtcars[mtcars$cyl==4,],
filter(mtcars, cyl==4),
subset(mtcars, cyl==4)
)
ggplot2::autoplot(bm)
## ---------------------------------------------------------------------------------------
df <- subset(mtcars, select=c(mpg, wt, hp, carb))
df$model <- rownames(mtcars)
head(df)
## ---------------------------------------------------------------------------------------
fit <- lm(mpg ~ wt + hp, data=df)
summary(fit)
## ---------------------------------------------------------------------------------------
plot(df$mpg, col="red", ylim=c(0,200))
points(df$wt, col="green")
points(df$hp, col="blue")
## ---------------------------------------------------------------------------------------
plot(df$carb, df$mpg, col="red", ylim=c(0,200))
points(df$carb, df$wt, col="green")
points(df$carb, df$hp, col="blue")
## ---------------------------------------------------------------------------------------
df.long <- data.frame()
a <- data.frame(model=df$model, val=df$mpg, name="mpg", carb=df$carb)
df.long <- rbind(df.long, a)
a <- data.frame(model=df$model, val=df$wt, name="wt", carb=df$carb)
df.long <- rbind(df.long, a)
a <- data.frame(model=df$model, val=df$hp, name="hp", carb=df$carb)
df.long <- rbind(df.long, a)
df.long
## ---------------------------------------------------------------------------------------
ggplot(df.long, aes(x=model, y=val, col=name)) + geom_point()
## ---------------------------------------------------------------------------------------
ggplot(df.long, aes(x=model, y=val, col=name)) + geom_point() +
facet_wrap(~name)
## ---------------------------------------------------------------------------------------
df.long2 <-
pivot_longer(
df,
cols = c("mpg", "wt", "hp"),
names_to = "name",
values_to = "val"
)
## ---------------------------------------------------------------------------------------
df.long2
## ---------------------------------------------------------------------------------------
ggplot(df.long, aes(x=model, y=val, col=name)) + geom_point() +
facet_wrap(~name)
## ---------------------------------------------------------------------------------------
df.long$name <- factor(df.long$name, levels=c("mpg", "wt", "hp"))
ggplot(df.long, aes(x=model, y=val, col=name)) + geom_point() +
facet_wrap(~name) +
theme(axis.text.x=element_blank())
## ---------------------------------------------------------------------------------------
pivot_wider(
df.long,
names_from = "name",
values_from = "val"
)
## ----results="hide"---------------------------------------------------------------------
df.long %>%
pivot_wider(
names_from = "name",
values_from = "val"
)