-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-Intermediate_R.Rmd
More file actions
569 lines (398 loc) · 15.2 KB
/
02-Intermediate_R.Rmd
File metadata and controls
569 lines (398 loc) · 15.2 KB
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# Intermediate R
<https://learn.datacamp.com/courses/intermediate-r>
## Conditionals And Control Flow
Reminder: `==` is for comparison and `=` is for assignment.
`TRUE` is treated as `1` for arithmetic, and `FALSE` is treated as `0`.
**Compare Vectors and Matrices**
<center>**Vectors**</center>
Number of views on each site:
```{r}
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
```
To find out which had more views:
```{r}
linkedin >= facebook
```
<center>**Matrices**</center>
Compare data in matrices:
```{r}
views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)
# When is views less than or equal to 14?
views <= 14
```
**Logical Operators**
<center>**& and |**</center>
With "Or" function: `|', only one condition (the right one or the left one) needs to be satisfied to spit out `TRUE`:
```{r}
socks <- 13
# Is last under 5 or above 10?
socks < 5 | socks >10
```
With "And" function: "&", both conditions (the right and left ones) need to be satisfied to spit out `TRUE`. In this case, the "left condition" is not satisfied, hence:
```{r}
# Is last between 15 (exclusive) and 20 (inclusive)?
socks > 15 & socks <= 20
```
<center>**"!" Operator**</center>
The `!` operator negates a logical value:
```{r}
x <- 5
y <- 7
!((x < 4) & !(y > 12))
```
This can be a brain-twister. Both `x` and `y` element is `FALSE`. However, there's a `!` in front of `y`, therefore, `(y-12)` is `TRUE`. So, within the entire `()`, a `FALSE` and `TRUE` would spit out a `FALSE` because one component is `FALSE`. Then, the `!` operator would flip the result, which becomes `TRUE` in the end.
<center>**The "IF" Statement**</center>
`IF `function allows the logic of "if this happens then do A." For example:
```{r}
num_views <- 14
if (num_views > 15) {
print("You're popular!")
}
```
This is the expanded of the above logic thought: "if this happens then do A, if doesn't, then do B, and if both doesn't happen, then do the C":
```{r}
if (num_views > 15) {
print("You're popular!")
} else if (num_views <= 15 & num_views > 10) {
print("Your number of views is average")
} else {
print("Try to be more visible!")
}
```
Logic are lawless inside "if-else" constructs. This is a good example from Datacamp:
if (number < 10) {
if (number < 5) {
result <- "extra small"
} else {
result <- "small"
}
} else if (number < 100) {
result <- "medium"
} else {
result <- "large"
}
print(result)
## Loops
**"While" Loop**
`While` function's logic is "while this is true, keep doing the task". Example from datacamp to understand:
```{r}
speed <- 64
# Extend/adapt the while loop
while (speed > 30) {
print(paste("Your speed is",speed))
if (speed > 48) {
print("Slow down big time!")
speed <- speed - 11
} else {
print("Slow down!")
speed <- speed - 6
}
}
```
**"For" Loop**
<center>**Loop A Vector, List**</center>
This loop will print out all the `views` listed in the `linkedin` vector orderly from left to right:
```{r}
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
# Loop version 1
for (views in linkedin) {
print(views)
}
```
This loop will also print out all the views from the vector, but it does so by reffering to the location of the specific element within the vector to print out:
```{r}
# Loop version 2
for (i in 1:length(linkedin)) {
print(linkedin[i])
}
#Note: use "[[ ]]" to select the elements in loop version 2 when looping a list.
```
<center>**Loop A Matrix**</center>
A `for` loop inside a `for` loop is called a `nested` loop, example from Datacamp:
```{r}
ttt <- matrix(c( "O", NA, "X", NA, "O", "O", "X", NA, "X"), byrow = TRUE, nrow =3)
for (i in 1:nrow(ttt)) {
for (j in 1:ncol(ttt)) {
print(paste("On row", i, "and column", j, "the board contains", ttt[i,j]))
}
}
```
**"Break" And "Next"**
The `break` terminate the running code if the condition is `FALSE`.
The `next` allow the code to the run after `break`. It skip over the element that made the code `FALSE` then continue.
```{r}
likes <- c(16, 9, 13, 5, 2, 17, 14)
for (heart in likes) {
if (heart > 10) {
print("You're popular!")
} else {print("Be more visible!")
}
if (heart > 16) {print("This is ridiculous, I'm outta here!")
break
}
if(heart < 5) {print("This is too embarrassing!")
next
}
print(heart)
}
```
## Functions
A way to see the components of a function is the args() function:
```{r}
args(sum)
```
**Exclude "NA" From A Calculation**
```{r}
linkedin <- c(16, 9, 13, 5, NA, 17, 14)
facebook <- c(17, NA, 5, 16, 8, 13, 14)
# Basic average of linkedin
mean(linkedin)
# Advanced average of linkedin
mean(linkedin, na.rm = TRUE)
```
The default setting in the `mean()` function is `na.rm = FALSE`, which means it doesn't exclude the `NA` variables. However, when switched to `TRUE`, the function excludes the `NA` varaibles.
**When Is It Required?**
mean(x, trim = 0, na.rm = FALSE, ...)
`x` is required; if you do not specify it, R will throw an error. `trim` and `na.rm` are optional arguments: they have a default value which is used if the arguments are not explicitly specified.
***Create A Function***
To create a `function`, assign a variable the function `function(condition){body}`:
```{r}
pow_two <- function(x) {
y <- x ^ 2
print(paste(x, "to the power two equals", y))
return(y)
}
pow_two(6)
#NOTE: "y" was defined inside the "pow_two()" function and therefore it is not accessible outside of that function. This is also true for the function's arguments of course - "x" in this case.
```
**Internal Variables of "Function()" are FIXED**
An external varaible can't be entered in to change the internal make-up of a created function:
```{r}
triple <- function(x) {
x <- 3*x
x
}
#Testing whether R updates the variable "a":
a <- 5
triple(a)
a
```
Even though the function `triple(a)` outputted 15, R didn't print the new `a` variable as `15` but as `5`.
**Example from Datacamp:**
```{r}
likes <- c(10, 18, 4)
interpret <- function(num_views) {
if (num_views > 15) {
print("You're popular!")
return(num_views)
} else {
print("Try to be more visible!")
return(0)
}
}
interpret(likes[2])
interpret(likes[3])
```
**Load an R Package**
There are basically two important functions when it comes to R packages:
`install.packages()` installs a given package.
`library()` which loads packages, i.e. attaches them to the search list on your R workspace.
**Anonymous functions**
An anonymous function is a function that's NOT aasigned a variable(name):
```{r}
# Named function
triple <- function(x) { 3 * x }
# Anonymous function with same implementation
function(x) { 3 * x }
```
## The apply family
**"lapply()"***
`lappy()` applies the function inputted inside the `()` over a vector or list, and spit out a list.
lapply(X, FUN, ...)
For example:
```{r}
numbers_list <- list(c(17, 28, -2, 9, 22), c(2, -19, 54, 27, 11), c(91, 76, -34, 8, 10))
extremes_avg <- function(x) {
( min(x) + max(x) ) / 2
}
# Apply extremes_avg() over numbers_list using lapply()
lapply(numbers_list, extremes_avg)
```
**"sapply()"***
`sapply()` applies the function inputted inside the `()` over a vector or list, and *try* to arrange the resulting list into an organized array. If not possible, `sapply()` will return the same list as `lapply()` spit out.
sapply(X, FUN, ...)
Continuing the example above:
```{r}
# Apply extremes_avg() over numbers_list using sapply()
sapply(numbers_list, extremes_avg)
```
The outputted result looks more compact than `lapply()`.
**"vapply()"***
`vapply()` applies the function inputted inside the `()` over a vector or list like `lapply()` or `sapply()`. However, with `vapply()`, it requires a specified output format, meaning tell it what result type it should spit out:
vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)
The `FUN.VALUE` argument expects a template for the return argument of this function `FUN.` `USE.NAMES` is `TRUE` by default; in this case `vapply()` tries to generate a named array, if possible.
Example:
```{r}
# Definition of the basics() function
basics <- function(x) {
c(min = min(x), mean = mean(x), median = median(x), max = max(x))
}
# Fix the error:
vapply(numbers_list, basics, numeric(4))
```
In this example, if `numerics` specified was `3` instead of `4`, the code would **NOT** run and give an error because `vapply()` function requires a specific output format. In this case, the `basics` function has 4 elements: `min`, `mean`, `median`, and `max`, therefore, `vapply()` need to specified as `numeric(4)`.
## Utilities
**Mathematical Utilities**
`abs()` : Calculate the absolute value.
`sum()` : Calculate the sum of all the values in a data structure.
`mean()` : Calculate the arithmetic mean.
`round()`: Round the values to 0 decimal places by default.
Example:
```{r}
digits <- c(1.9, -2.6, 4.0, -9.5, -3.4, 7.3)
# Sum of absolute rounded values of errors
sum(abs(round(digits)))
```
**Data Utilities**
`seq()`: Generate sequences, by specifying the `from`, `to`, and `by` arguments.
`rep()`: Replicate elements of vectors and lists.
```{r}
rep(seq(1, 7, by = 2), times = 7)
```
`sort()`: Sort a vector in ascending order by default. Works on numerics, but also on character strings and logicals.
`rev()`: Reverse the elements in a data structures for which reversal is defined.
`str()`: Display the structure of any R object.
`append()`: Merge vectors or lists.
`is.*()`: Check for the class of an R object.
`as.*()`: Convert an R object from one class to another.
`unlist()`: Flatten (possibly embedded) lists to produce a vector.
Example:
```{r}
linkedin <- list(16, 9, 13, 5, 2, 17, 14)
facebook <- list(17, 7, 5, 16, 8, 13, 14)
# Convert linkedin and facebook to a vector: li_vec and fb_vec
li_vec <- unlist(linkedin)
fb_vec <- unlist(facebook)
# Append fb_vec to li_vec: social_vec
social_vec <- append(li_vec, fb_vec)
# Sort social_vec
sort(social_vec, decreasing = TRUE)
```
**Regular Expressions**
Regular expressions can be used to see whether a pattern exists inside a character string or a vector of character strings.
<center>**"grep()" and "grepl()"**</center>
`grepl()`: the `l` in `grepl()` stands for logical, which indicates that this function returns `TRUE` when a pattern is found in the corresponding character string.
`grep()`: returns a vector contains the location of the character strings(by order) that contains the pattern searched for.
The caret: `^`, to match the content located in the start of a string.
The dollar-sign: `$`, to match the content located in the end of a string.
```{r}
emails <- c("john.doe@ivyleague.edu", "education@world.gov", "dalai.lama@peace.org",
"invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
#Search for the email addresses in the vector above that contains "@", anything in between, and "edu":
grepl(pattern = "@.*\\.edu$", emails)
grep(pattern = "@.*\\.edu$", emails)
# Subset emails using hits
emails[grep(pattern = "@.*\\.edu$", emails)]
```
`.*`: can be read as "any character that is matched zero or more times".Both the dot and the asterisk are metacharacters.
`\\`: is like a cut-off. It put a separation wall between the `.*` and `.edu`.
<center>**"sub()" and "gsub()"**</center>
`sub()` and `gsub()`can specify a `replacement` argument. If inside the character vector `x`, the regular expression `pattern` is found, the matching element(s) will be replaced with `replacement`.`sub()` only replaces the first match, whereas `gsub()` replaces all matches.
```{r}
emails <- c("john.doe@ivyleague.edu", "education@world.gov", "dalai.lama@peace.org",
"invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
sub(pattern = "@.*\\.edu$", replacement = "@datacamp.edu", emails)
#The [1] and [5] elements has been changed.
```
`\\s`: Match a space. The "s" is normally a character, escaping it `\\` makes it a metacharacter.
[0-9]+: Match the numbers 0 to 9, at least once (+).
([0-9]+): The parentheses are used to NOT confuse the `pattern` matching criterias.
The `\\1`: is to input the regular expression `[0-9]+` matched into the `replacement` argument.
```{r}
awards <- c("Won 1 Oscar.", "Another 9 wins & 24 nominations.",
"2 wins & 3 nominations.",
"Nominated for 2 Golden Globes. 1 more win & 2 nominations.")
sub(".*\\s([0-9]+)\\snomination.*$", "\\1", awards)
```
The logic behind the `pattern` criteria is as follow: skip any character and then a space between the number and the word "nomination". Then, skip again any character after the word "nomination".
**Date And Time**
Dates are represented by `Date` objects. Times are represented by `POSIXct` objects.
However, dates and times are simple numerical values. `Date` objects store the number of *days* since the 1st of January in 1970. `POSIXct` store the number of *seconds* since the 1st of January in 1970.
```{r}
# Get the current date: today
today <- Sys.Date()
today
# See what today looks like under the hood
unclass(today)
# Get the current time: now
now <- Sys.time()
now
# See what now looks like under the hood
unclass(now)
```
<center>**Date Formats**</center>
Use the `as.Date()` function to create a `Date` object from a simple character string.
%Y: 4-digit year (1982)
%y: 2-digit year (82)
%m: 2-digit month (01)
%d: 2-digit day of the month (13)
%A: weekday (Wednesday)
%a: abbreviated weekday (Wed)
%B: month (January)
%b: abbreviated month (Jan)
```{r}
# Definition of character strings representing dates
str1 <- "May 23, '96"
str2 <- "2012-03-15"
str3 <- "30/January/2006"
# Convert the strings to dates: date1, date2, date3
date1 <- as.Date(str1, format = "%b %d, '%y")
date2 <- as.Date(str2, format = "%Y-%m-%d")
date3 <- as.Date(str3, format = "%d/%B/%Y")
# Convert dates to formatted strings
format(date1, "%A")
format(date2, "%d")
format(date3, "%b %Y")
```
<center>**Date Calculations**</center>
Both `Date` and `POSIXct` objects are represented by simple numerical values under the hood.
```{r}
today <- Sys.Date()
today + 1
today - 1
as.Date("2015-03-12") - as.Date("2015-02-27")
```
<center>**Time Formats**</center>
Use `as.POSIXct()` to convert a character string to a `POSIXct` object.
%H: hours as a decimal number (00-23)
%I: hours as a decimal number (01-12)
%M: minutes as a decimal number
%S: seconds as a decimal number
%T: shorthand notation for the typical format %H:%M:%S
%p: AM/PM indicator
For a full list of conversion symbols, consult the `strptime` documentation in the console: `?strptime`
```{r}
# Definition of character strings representing times
str1 <- "May 23, '96 hours:23 minutes:01 seconds:45"
str2 <- "2012-3-12 14:23:08"
# Convert the strings to POSIXct objects: time1, time2
time1 <- as.POSIXct(str1, format = "%B %d, '%y hours:%H minutes:%M seconds:%S")
time2 <- as.POSIXct(str2, format = "%Y-%m-%d %H:%M:%S")
# Convert times to formatted strings
format(time1, "%M")
format(time2, "%I:%M %p")
```
<center>**Time Calculations**</center>
Examples of doing calculations with `POSIXct` objects:
```{r}
now <- Sys.time()
now + 3600 # add an hour
now - 3600 * 24 # subtract a day
```
```{r}
birth <- as.POSIXct("1879-03-14 14:37:23")
death <- as.POSIXct("1955-04-18 03:47:12")
einstein <- death - birth
einstein
```