forked from tpemartin/110-1-r4ds-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
week14.rmd
460 lines (358 loc) · 8.79 KB
/
week14.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
---
title: "week14"
output: html_notebook
---
### 4.6.4
```{r}
hobby = c(
'sport, reading, movie',
'sport',
'movie, sport, reading',
'movie, Reading',
'sport')
```
Who likes to read?
```{r}
hobby == "reading"
'sport, reading, movie' != "reading"
'sport, reading, movie' == 'sport, reading, movie'
hobby %in% "reading"
'sport, reading, movie' %in% 'sport, reading, movie'
```
```{r}
stringr::str_detect(
string, # 'sport, reading, movie
pattern # 'reading'
)
```
```{r}
# any one likes to read
stringr::str_detect(
hobby, # string
"reading" # pattern
) # 4th is FALSE
stringr::str_detect(
hobby, # string
stringr::coll("reading", ignore_case = T) # pattern
) # 4th is TRUE
```
```{r}
table(hobby)
```
```{r}
stringr::str_split(
string,
pattern
)
```
```{r}
stringr::str_split(
hobby[[1]],
pattern = ", " # ", " != ","
) -> hobby1
stringr::str_split(
hobby[[2]],
pattern = ", "
)
stringr::str_split(
hobby[1:2],
pattern = ", "
) -> hobby12
class(hobby12)
# table(an atomic vector)
## turn a list into an atomic vector
table(unlist(hobby12))
```
```{r}
# Count for each hobby
table(hobby)
unlisted_hobbies <- {
hobby |>
stringr::str_split(pattern = ", ") -> list_hobbies
unlist(list_hobbies)
}
table(unlisted_hobbies)
```
```{r}
unlisted_hobbies
toupper(unlisted_hobbies) |> table()
tolower(unlisted_hobbies) |> table()
```
```{r}
# any one likes to read
pick_reading1 <-
stringr::str_detect(hobby, "reading") # 4th is FALSE
pick_reading2 <-
stringr::str_detect(hobby, stringr::coll("reading", ignore_case = T)) # 4th is TRUE
```
```{r}
pick_reading2
which(pick_reading2)
```
***
```{r}
df_dates =
data.frame(
year = c('2001','2001','2002','2001','2001'),
month = c('4','10','1','1','4'),
day = c('3','3','22','18','3')
)
```
```{r}
df_dates$year
df_dates$month
df_dates$day
df_dates$data <- lubridate::ymd(c(
"2001 4 3", "2001 10 3", "2002 1 22", "2001 1 18", "2001 4 3"
))
df_dates$date |> class()
```
```{r}
chr_dates <- paste(
df_dates$year,
df_dates$month,
df_dates$day,
sep="-"
)
chr_dates
dates <- lubridate::ymd(chr_dates)
dates |> class()
```
#### factor vector
```{r}
students <-
data.frame(
major = c('economics','sociology','economics','sociology','sociology','finance','sociology','statistics','statistics','sociology'),
year= c(4,1,4,3,1,4,4,2,1,3),
credits= c(16,13,10,21,17,12,21,15,20,17)
)
```
* school (學院)
```{r}
students$major |> unique()
students$major
school <- character(10)
school[c(1, 2, 3, 4, 5, 7, 10)] <- "social science"
school[c(6, 8, 9)] <- "business"
school
```
```{r}
school <- character(10)
{
# For those whose major is economics or sociology, their school is social science.
pick_those = students$major %in%
c("economics", "sociology")
school[pick_those] = "social science"
# For those whose major is statistics or finance, their school is business
pick_those = students$major %in%
c("statistics", "finance")
school[pick_those] = "business"
}
school
```
```{r}
school = factor(students$major)
{
levels(school) <-
c("social science","business","social science","business") # RHS and LHS must have the same length
school
}
school
```
```{r}
students$year
students$year <- factor(students$year)
students$year
levels(students$year) <- c(
"freshman", "sophemore", "junior", "senior"
)
students$year
```
numerical vecrtor into a categorical vector based on some interval definitions
```{r}
students$credits
```
credits: <= 12 (light), 13-20 (normal), 20+ (heavy)
```{r}
workload <- character()
workload[c(3, 6)] <- "light"
workload[c(1, 2, 5, 8, 9, 10)] <- "normal"
workload[c(4, 7)] <- "heavy"
workload
```
```{r}
workload <- character(10)
pick_those = students$credits <= 12
workload[pick_those] = "light"
pick_those = (students$credits >= 13) | (students$credits <= 20)
workload[pick_those] = "normal"
pick_those = students$credits > 20
workload[pick_those] = "heavy"
workload
```
```{r}
workload <- factor(students$credits)
levels(workload) <- c(rep("light", 2),rep("normal", 5) , "heavy")
workload
```
* credits: <= 12 (light), 13-20 (normal), 20+ (heavy)
* light: 12, normal: 20, heavy: 30)
```{r}
maximalValues <- c(0, 12, 20, 30)
workload <-
cut(
students$credits, # a numerical vector
maximalValues # cut point
)
workload # already a factor
students$credits
levels(workload) <- c("light", "normal", "heavy")
workload
students$credits
```
```{r}
credits2 <- c(0, students$credits)
credits2
cut(
credits2,
maximalValues
)
```
```{r}
cut(
credits2,
maximalValues,
include.lowest = T
) -> workload
levels(workload) <- c("light", "normal", "heavy")
workload
```
## 4.7 Summarise one vector
```{r}
dates <- c('2016-11-15','NA','NA','1997-05-07','1995-08-25','2002-09-20','NA','NA','NA','1995-07-16', '2011-06-22')
grades <- c(29,53,26,27,55,69,NA,NA,63,NA,56)
genders <- c('Male','Female','Male','Male','Female','Female',NA,'Male','Male','Female','Female')
majors <- c('economics','economics',NA,'economics','economics','economics','economics','statistics','law','economics','law')
dataSet <-
data.frame(
date=dates,
grade=grades,
gender=genders,
major=majors
)
analysis <- list()
analysis$data <- dataSet
```
```{r}
View(dataSet)
```
```{r}
dataSet$date |> class()
dataSet$date |> lubridate::ymd() -> dataSet$date
dataSet$date |> class()
analysis$data <- dataSet
```
* analysis
```{r}
anyNA(dates)
dates |> is.na() |> sum() -> analysis$dates$na$sum
anyNA(grades)
grades |> is.na() |> sum() -> analysis$grades$na$sum
saveRDS(analysis, file = "analysis.Rds") # turn off computer
```
* only need to save ONE object
* a list gives me ability to use menu to manage my analysis process.
```{r}
analysis = readRDS("analysis.Rds")
analysis$grades$na$sum
analysis$data -> dataSet
```
```{r}
dates |> is.na() |> sum() -> a
grades |> is.na() |> sum() -> b
save(a, b, file="mywork.Rdata") # turn off computer
```
```{r}
load("mywork.Rdata")
```
***
Next day
```{r}
analysis = readRDS("analysis.Rds")
dates <- analysis$data$date
View(analysis$data)
```
```{r}
class(dates)
typeof(dates)
dates |> range() # minimal and maximal
dates |> range(na.rm=T) -> analysis$dates$range
analysis$dates$range
grades <- analysis$data$grade
grades |> range(na.rm=T) -> analysis$grades$range
analysis$grades$range
grades |> median(na.rm=T) -> analysis$grades$median
analysis$grades$median
grades |> mean(na.rm=T) -> analysis$grades$mean
analysis$grades$mean
```
```{r}
length(grades)
analysis$grades$na$sum
```
We have 11 students in the data set. For their grades, 3 are missing. The median grade is 54 (which means among 8 students with grades, 4 are lower than 54; 4 are higher than 54). The average grade is 47.25.
```{r}
genders <- analysis$data$gender
genders |> class()
genders |> factor() -> genders
genders |> levels() # only works for factor
```
```{r}
majors <- analysis$data$major
majors |> class()
majors |> unique() # returns a vector, data frame or array like x but with duplicate elements/rows removed.
```
```{r}
majors |> is.na() |> sum() -> analysis$majors$na$sum
majors |> factor() -> majors -> analysis$data$major
levels(majors)
```
* majors feature.
There are 11 students. One's major is missing. The remaining 10 students have majors from economic (7 students), law (2 students) and statistics(1 student).
```{r}
table(majors)
saveRDS(analysis, file="analysis.Rds")
```
```{r}
dataSet0 <-
data.frame(
dates = c('2016-11-15','1997-05-07','NA','NA','1997-05-07','1995-08-25','2002-09-20','NA','NA','NA','1995-07-16', '2011-06-22', '2016-11-15'),
grades = c(29,27, 53,26,27,55,69,NA,NA,63,NA,56, 29),
genders = c('Male','Male', 'Female','Male','Male','Female','Female',NA,'Male','Male','Female','Female','Male'),
majors = c('economics','economics', 'economics',NA,'economics','economics','economics','economics','statistics','law','economics','law','economics')
)
View(dataSet0)
```
```{r}
View(dataSet0)
duplicated(dataSet0)
whichIsDuplicated <- which(duplicated(dataSet0))
dataSet0[whichIsDuplicated, ]
dataSet0[whichIsDuplicated, ] -> dataSet0
View(dataSet0)
```
```{r}
dataSet0 <- unique(dataSet0)
View(dataSet0)
```
```{r}
# na is removed before table summarisation
genders |> table()
# preliminary summary should include NA summary
genders |> table(useNA = "always") -> analysis$genders$table
analysis$genders$table
```
```{r}
analysis = readRDS("analysis.Rds")
analysis$genders$table
```