-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclass_2.Rmd
435 lines (335 loc) · 6.91 KB
/
class_2.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
---
title: "in class coding"
author: "Julia Gallucci"
date: "12/12/2023"
output: html_document
---
Types in R
```{r}
typeof("welcome") #character
typeof(FALSE) #logical
typeof(3.14) #double
typeof(10L) #integer
typeof(10) #double
typeof(10+3i) #complex
```
Logical Vectors
```{r}
logical_vector <- c(TRUE, TRUE, FALSE)
typeof(logical_vector)
compare_vector <- 1:5 %% 2 == 0
typeof(compare_vector)
compare_vector
```
Numeric Vectors
```{r}
double_vector <- c(3.1, -73, 2700)
typeof(double_vector)
length(double_vector)
integer_vector <- c(3L, -73L, 2700L)
typeof(integer_vector)
length(integer_vector)
special_values <- c(-1, 0 , 1 , NA) / 0
special_values
is.finite(special_values)
is.infinite(special_values)
is.na(special_values)
is.nan(special_values)
```
Character Vectors
```{r}
character_vector <- c("hello", "world", "2,000")
character_vector
length(character_vector)
typeof(character_vector)
```
Explicit Coercion
```{r}
character_vector <- c("1","0","1")
typeof(character_vector)
numeric_vector <- as.numeric(character_vector)
typeof(numeric_vector)
as.double(character_vector)
as.integer(character_vector)
logical_vector <- as.logical(numeric_vector)
logical_vector
```
Implicit coercion
```{r}
numeric_vector <- 1:10
numeric_vector
#which are greater than 4
logical_vector <- numeric_vector > 4
typeof(logical_vector)
#1 is coded as TRUE, 0 as FALSE
sum(logical_vector)
mean(logical_vector)
```
Mix type of vectors
```{r}
mix_vector <- c(TRUE, FALSE, 10L)
typeof(mix_vector)
mix_vector <- c(1L,4L, 1.5)
typeof(mix_vector)
mix_vector <- c(1.5,-3.5, "a")
typeof(mix_vector)
```
Using tidyverse
```{r}
library(tidyverse)
is_logical(c(TRUE,FALSE))
is_double(c(1.2,1.5))
is_integer(c(5L,10L))
is_character(c("cat","dog"))
```
vector recycling
```{r}
1:5
1:10
1:5 + 1:10
```
Name vectors
```{r}
name_vectors<- c(
a = 100,
b = 90,
c = 80,
d = 70,
e = 60
)
name_vectors
name_vectors[3]
name_vectors[c(3,3,4)]
name_vectors[c(-1,-2,-5)]
name_vectors[c(TRUE,TRUE,FALSE,TRUE,FALSE)]
name_vectors[name_vectors %% 20 == 0 ]
name_vectors[c("a","c")]
```
List
```{r}
mylist <- list(7,"abc",FALSE)
mylist
str(mylist) #structure of our list
mylist <- list(a = 1:4,
b = "zyx",
c = list(-1,-5))
mylist
mylist[[2]]
mylist[["b"]]
mylist$b
```
Tibble
```{r}
mytibble <- tibble(x = 1:5,
y = 1,
z = x^2 + y)
mytibble
```
```{r}
data("iris")
head(iris)
iris_tibble <- as_tibble(iris)
iris_tibble
```
subset a data frame
```{r}
iris$Species
iris[["Species"]]
iris %>%
.$Species
iris %>%
.[["Species"]]
```
Strings
```{r}
library(stringr)
"this"
'this'
str_length("This is a string")
str_c("This is a string","6")
str_sub("This is a string",start = 7,end = 12)
str_to_lower("UPPER CASE")
str_to_upper("lower case")
str_to_title("no capitalization")
mystring <- c("apple","banana",
"clementime","dragonfruit")
str_view(mystring, pattern = "an")
str_view(mystring, pattern = ".a.")
str_view(mystring, pattern = "^a")
str_view(mystring, pattern = "a$")
str_view(mystring, pattern = "[^a,e,u]")
mystring <- "abcccdeee"
str_view(mystring, pattern = "cc*")
mystring <- c("abab", "cdcd", "efgh")
str_view(mystring, pattern = "(..)\\1", match = TRUE)
mystring <- c("banana", "dodo", "apple")
?str_detect()
str_detect(mystring, pattern = "(..)\\1")
str_subset(mystring, pattern = "(..)\\1")
str_count(mystring, pattern = "(..)\\1") # TRUES are coded as 1, FALSE as 0
```
Factors
```{r}
library(forcats)
months <- c("Dec","Apr", "Jan","Mar")
months
month_levels <- c(
"Jan","Feb","Mar","Apr",
"May", "Jun","Jul","Aug",
"Sep","Oct","Nov","Dec"
)
month_fix <- factor(months, levels = month_levels)
month_fix
#fct_recode(data, New name = Old name)
fct_recode(month_fix, "December" = "Dec")
```
Dates
```{r}
today()
now()
library(lubridate)
#as_datetime(<POSIXct item>)
ymd("2017-01-31")
ymd(20170131)
mdy("January 31st, 2017")
dmy("31-Jan-2017")
today()
as_datetime(today())
now()
as_date(now())
datetime <- ymd_hms("2016-07-08 12:34:56")
year(datetime)
month(datetime)
mday(datetime)
yday(datetime)
wday(datetime)
hour(datetime)
minute(datetime)
second(datetime)
```
Time spans
```{r}
today() - ymd(20000101)
as.duration(today() - ymd(20000101))
dseconds(120)
dminutes(60)
dhours(c(12,24))
ddays(0:7)
dweeks(4)
dyears(10)
#time periods
today() + years(1)
today() + months(1)
today() + days(1)
today() + hours(1)
today() + minutes(1)
today() + seconds(1)
#time zones
ymd_hms("2021-01-01 12:00:00", tz = "America/New_York")
ymd_hms("2021-01-01 12:00:00", tz = "Europe/Copenhagen")
ymd_hms("2021-01-01 12:00:00", tz = "Pacific/Auckland")
```
Missing data
```{r}
NA > 5
NA == 10
NA + 5
NA == NA
#How we detect missing values
is.na(NA)
```
Exercises
1. Make a tibble where the vectors do not have equal length. What happens?
```{r}
mytibble <- tibble(
A = 1:10,
B = 1:2
)
mytibble
#Tibbles always have to have vectors (columns) of equal lengths
```
2. In the following tibble, extract variable B:
```{r}
mytibble <- tibble(
A = 1:10,
B = A * 2
)
mytibble
#using the column name "B"
mytibble[["B"]]
mytibble$B
mytibble %>%
.$B
#use the column index 2
mytibble[[2]]
mytibble$A
mytibble[[1]]
mytibble[["A"]]
mytibble
# data[row,column]
mytibble[2,2]
mytibble[2,]
mytibble[,2]
mytibble[c(1:4),]
mytibble[-c(1:4),]
```
3. Try using functions paste() and paste0(). Compare them to str_c(), how do they work differently?
```{r}
?paste
?paste0
?str_c
paste("Hi","my", "name","is","julia", sep = " ")
paste0("Hi","my", "name","is","julia")
#similarly
paste("Hi","my","name","is","julia", sep = "")
str_c("Hi","my", "name","is","julia")
#default separator is nothing, same as paste0
```
4. Look up the function str_trim() and demonstrate application.
```{r}
#Michael's example:
#remove white space on both sides
teststring <-"the string"
newstring <-str_pad(teststring,50,side=c("both"))
newstring
str_trim(newstring)
#Julia's example:
str_trim("Hi my name is Julia ")
#FM's example:
str_trim(" Hello world ")
```
5. Given the text "Hello, world! \\." match the sequence "\" with a regular expression
```{r}
text <- "Hello, world! \\."
#Henry's answer:
str_view(text, pattern = "\\\\")
```
6. Given the text "x-ray" match words that start with x with a regular expression
```{r}
text <- c("x-ray", "cat", "excellent")
str_view(text,pattern = "^x")
```
7. Given the text "cat, hat, dog, rat" match words that contain the regular expression at
```{r}
text <- c("cat", "hat", "dog", "rat")
str_view(text, pattern = ".at")
str_view(text, pattern = "at")
```
8. Given the text LETTERS (A-Z), match only those of vowels with a regular expression
```{r}
LETTERS
letters
str_view(LETTERS, pattern = "[A,E,I,O,U]") #vowels
str_view(LETTERS, pattern = "[^A,E,I,O,U]") #everything except vowels
```
9. What does ^.*$ match?
```{r}
str_view(1:10, "^.*$")
text <- "Hello world"
str_view(text, "^.*$")
#EVERYTHING!
```
10. What would happen if we parse a string with invalid dates?
```{r}
ymd("2023-02-30")
```