-
Notifications
You must be signed in to change notification settings - Fork 3
/
fundamentals.Rmd
754 lines (572 loc) · 17.9 KB
/
fundamentals.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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
---
title: Fundamentals
weight: 2
output:
blogdown::html_page:
toc: true
---
This page provides a very brief introduction to general R programming, and is sufficient to perform very complicated computations. It is certainly not comprehensive, however, and it is expected that one will need to supplement the information here with other material, and some healthy experimentation.
## Variables and values
A value can be assigned to a variable using the assignment operator `<-`. The variable is created if it doesn't already exist.
```{r}
x <- 123
```
Creating or assigning a variable a value does not produce any output. Providing the name of the variable as input does produce its value as output.
```{r}
x
```
One can reassign the value of `x` using `<-`. The type (e.g. numeric, string) of the variable need not be the same as before.
```{r}
x <- "hello"
x
```
One can use the operator `=` instead of `<-`, but this is not recommended. In RStudio **Alt** + **-** is a keyboard shortcut for writing the assignment operator.
A valid variable name consists of letters, numbers and the dot `.` or underscore `_` characters. Visible variable names must start with a letter.
## Arithmetic
As you would expect, one can add, subtract, multiply, divide and exponentiate. There are also operators for integer division and remainder (modulus).
| Operator | Meaning |
|:--------:|:----------------:|
| + | add |
| - | subtract |
| * | multiply |
| / | divide |
| ^ or ** | exponentiate |
| %/% | integer division |
| %% | integer modulus |
```{r}
2^3
7 %/% 3
7 %% 3
```
For convenience, these operators are used the same way we write them on paper. The alternative is to surround the operator with ticks.
```
`+`(2,3)
```
While this is natural, it is necessary in some circumstances to use parentheses or curly braces to ensure operations occur in the right order.
```{r}
2 + 3 * 5
(2 + 3) * 5
{2 + 3} * 5
```
## Conditional statements
Commands can be executed conditional upon a logical value (i.e. TRUE or FALSE) by using an if/else statement.
```{r}
if (TRUE) {
print("this is printed")
} else {
print("this is not printed")
}
if (FALSE) {
print("this is not printed")
} else {
print("this is printed")
}
```
It is not necessary to have an else statement.
```{r}
if (TRUE) {
print("this is printed")
}
if (FALSE) {
print("this is not printed")
}
```
One can nest if/else statements
```{r}
if (FALSE) {
print("this is not printed")
} else {
if (TRUE) {
print("this is printed")
}
}
if (FALSE) {
print("this is not printed")
} else if (TRUE) {
print("this is printed")
}
```
The curly braces `{` and `}` are necessary when there are multiple statements to be executed in the conditional block. For example, in the following code "1" is not printed, but "2" is (the indentation is just misleading).
```{r}
if (FALSE)
print(1)
print(2)
```
The above snippet is equivalent to
```{r}
if (FALSE) {
print(1)
}
print(2)
```
It is often considered good practice to use curly braces even for single-statement blocks, as it is easy to read and can be less likely to lead to bugs on modification.
The `ifelse` operator can also be useful in some situations. You can [read about it](https://adv-r.hadley.nz/control-flow.html#vectorised-if) after you are familiar with vectors in R.
## Relational and logical operators
Conditional statements are often used when the logical value is computed dynamically.
```{r}
x <- 5
if (x < 10) {
print("x is less than 10")
} else {
print("x is greater than or equal to 10")
}
```
The relational operators are
| Operator | Meaning |
|:--------:|:------------------------:|
| < | less than |
| > | greater than |
| <= | less than or equal to |
| >= | greater than or equal to |
| == | equals |
| != | not equals |
Logical operators are useful for combining logical values. These are
| Operator | Meaning |
|:--------:|:------------------------:|
| ! | not |
| & | and |
| \| | or |
| && | short-circuit and |
| \|\| | short-circuit or |
A short-circuit `&&` or `||` evaluates only those values from left to right that are required to determine if the result is `TRUE` or `FALSE`. For example, `FALSE && x` evaluates to `FALSE` and `TRUE || x` evaluates to `TRUE` irrespective of the value of `x` and so `x` need not be evaluated.
```{r}
x <- TRUE
y <- FALSE
if (!y) {
print("this is printed")
}
if (x & y) {
print("this is not printed")
}
if (x | y) {
print("this is printed")
}
if (x || NA) {
print("this is printed even though NA is not a logical value")
}
```
## Functions
Functions are incredibly useful building blocks for writing understandable programs, enabling code re-use and avoiding repetitions. The syntax for creating a function is
```{r}
miles2km <- function(x) {
# 1 mile is 1.609344 km
y <- x * 1.609344
return(y)
}
```
What is 500 miles in kilometres?
```{r}
miles2km(500)
```
What is 200 miles in kilometers?
```{r}
miles2km(200)
```
Once the `miles2km` function has been written once, it can be used many times.
Functions can have multiple arguments.
```{r}
hypotenuse <- function(a, b) {
return(sqrt(a^2 + b^2))
}
hypotenuse(3, 4)
```
There are many functions and constants that have already been defined in R's base package. These include the constant `pi`, trigonometric functions like `sin`, `cos`, `tan`, and functions like `log`, `exp`. The `log` function has a *default* second argument.
```{r}
log(100) # outputs natural log of 100
log(100, 10) # outputs log of 100 to base 10
log(100, base = 10) # outputs log of 100 to base 10
```
There are advanced techniques one can use to define and use functions, which you can research on your own. For example, one can call functions using named arguments, which can help avoid mistakes.
```{r}
foo <- function(a, b) {
return(10*a + b)
}
foo(4, 2)
foo(a=4, b=2)
foo(b=2, a=4)
```
While it is often not necessary to explicitly use `return`, it is usually considered good practice. An R function will return the last evaluated statement in the function.
```{r}
foo <- function(x) {
x + 1
}
foo(22)
```
The `return` command stops execution of that function and returns.
```{r}
foo <- function(x) {
return(x)
print("this is not printed")
}
foo(22)
```
Only one value can be returned by an R function. However, this value can be a vector, a list, or even another function.
## Getting help
One can access R's help documentation by opening the Help window in RStudio. One often wants to access the documentation for a specific function. One can do this by typing "?" followed by the function name in the R console.
```
?log
```
One can perform a keyword search using "??" instead of "?".
```
??logarithm
```
Of course, R is a popular language and there are also many resources online such as webpages, blogs and forums that can help solve problems.
## Vectors
We have focused on single values so far, for simplicity. In fact, a single number in R is a vector of size 1.
One can *combine* two vectors using the `c` function.
```{r}
x <- 1
y <- 2
z <- c(x, y)
z
w <- c(z, 3)
w
```
There are a few ways to quickly specify vectors.
```{r}
1:10
10:1
seq(1, 2, 0.1)
rep(42, 8)
```
Many basic R functions operate element-wise on vectors.
```{r}
a <- c(1, 3, 5, 7)
b <- c(2, 4, 6, 8)
a + b
a * b
a ^ b
log(a)
```
Other functions make sense for vectors of length greater than 1.
```{r}
x <- c(5, 2, 1, 6, 4, 1, 3)
sort(x)
min(x)
max(x)
sum(x)
mean(x)
var(x)
```
Vectors can be indexed using vectors of integers (although not necessarily with integer *type*).
```{r}
x <- 1:6
x[3]
x[c(2, 4)]
x[c(3, 5, 3)]
x[-c(2, 4)]
```
They can also be indexed using vectors of logicals.
```{r}
x <- 1:6
x[c(FALSE, TRUE, FALSE, TRUE, FALSE, FALSE)]
x[x %% 2 == 0]
```
Values can be changed using assignment.
```{r}
x <- 1:6
x[2] <- 42
x
x[c(2, 3)] <- c(11, 12)
x
```
## Vector types
So far we have focused on variables that are classified as `numeric` by R. There are 6 *atomic* vector types, and each vector can only have one of these types.
- logical
- integer
- numeric / double
- complex
- character
- raw
A single logical value takes either the value `TRUE` or `FALSE`.
An integer is specified by appending "L" to the end of the input. It is not that common to create integer vectors in R functions: most of the time one just uses numeric (i.e. double-precision floating point) vectors even when all the elements are mathematically integers.
```{r}
x <- 123L
typeof(x)
x <- 123
typeof(x)
```
A complex number is specified using `complex`. One can call the following functions on a complex number: `Re`, `Im`, `Mod`, `Arg`, `Conj`.
```{r}
x <- complex(real=1, imaginary=2)
x
Conj(x)
```
A variable of type character can be a string, not just a single character. In particular, a single string is a vector of length 1, not a vector of characters.
```{r}
x <- "hello"
typeof(x)
x <- "hello world"
length(x)
print(x)
x <- c("hello", "world")
length(x)
print(x)
```
We do not discuss the raw type here: it is used to store raw bytes.
## Matrices
Matrices can be created using the `matrix` command.
```{r}
x <- matrix(1:6, 2, 3)
x
```
The dimension of a matrix is given by `dim`.
```{r}
dim(x)
```
They can be indexed in a similar way to vectors.
```{r}
x[1,3]
x[2,]
x[2,c(TRUE, FALSE, TRUE)]
x[,3]
```
There are many built-in R functions for matrices. One important one is `%*%` for matrix multiplication (`*` is element-wise multiplication).
```{r}
A <- matrix(c(4, 2, 2, 4), 2, 2)
B <- matrix(c(1, 2, 3, 4), 2, 2)
A * B
A %*% B
```
```{r}
t(B) # transpose
eigen(B)
svd(B) # singular value decomposition
chol(A) # cholesky decomposition
diag(A)
```
One can add a row (resp. column) to a matrix using the `rbind` (resp. `cbind`) function.
```{r}
A <- diag(1:3)
A
rbind(A, 4:6)
cbind(A, 4:6)
```
An R `matrix` is equivalent a 2-dimensional `array`. One can create higher dimensional arrays.
```{r}
v <- array(1:24, c(2,3,4))
v[1,2,3]
```
An R `vector` is similar to, but not exactly the same as, a `matrix` with one column.
```{r}
matrix(1:2, 2, 1)
1:2
```
One can convert between the two using the `as.vector` and `as.matrix` commands.
## Lists
Vectors and matrices must contain variables of the same type. Lists are more generic data structures, and are sometimes called *generic* vectors.
List elements can be given names, which can be used to index the list using `$`. Lists can also be indexed using numbers inside `[[` and `]]`. New elements can be added by name or by indexing
```{r}
x <- list(numbers = 1:5, message = "hello")
x
x$numbers
x$message
x[[1]]
x[[2]]
x$even.numbers <- x$numbers[(x$numbers %% 2) == 0]
x[[4]] <- c(1.1, 2.2)
names(x)[[4]] <- "floats"
x
```
Lists can contain lists or functions as elements, so they can be very rich data structures.
```{r}
f <- list()
f$name <- "multiply"
f$arg.types <- list(x="numeric", y="numeric")
f$eval <- function(x, y) {
return(x*y)
}
f$name
f$arg.types
f$eval(6, 7)
```
## Basic scoping
R uses *lexical scoping*, which means that it resolves a variable name in a specific location in the code by considering where that variable is *defined* in the code. In particular, name resolution does not depend on the run time call stack of the program. We do not go into too many details here, except to show some examples that should help you become familiar with how scoping works in R.
A name introduced inside a function renders irrelevant any variables with the same name outside the function body.
```{r}
x <- 1
foo <- function() {
x <- 2
x
}
foo()
```
```{r}
x <- 1
foo <- function(x) {
x
}
foo(2)
```
If a name cannot be resolved in the "local scope", R will look one level up in the code.
```{r}
x <- 1
y <- 2
foo <- function() {
x <- 3
c(x, y)
}
foo()
```
The scoping rule determines *which* `y` is being referred to. The *value* associated with that `y` does depend on the execution context. In the below example, the name is resolved to the `y` in global scope, whose value changes between the two invocations of `foo`.
```{r}
x <- 1
y <- 2
foo <- function() {
x <- 3
c(x, y)
}
foo()
x <- 20
y <- 10
foo()
```
To see what is meant by "one level up", the following snippet may be helpful.
```{r}
x <- 1
foo <- function() {
x <- 2
bar <- function() {
x
}
bar()
}
foo()
```
Lexical scoping is now very common, and R's specific scoping rules are *usually* easy to work with.
A particular problem is when a programmer expects a variable to be defined locally within a function but it is not, and instead a global variable with the same name is used.
Curly braces `{` and `}` do not define a new scope in R.
## Pass by value semantics
In R one can think of arguments to functions as if they are passed by value. That is, when a function is called with an argument `x` it is as if `x` is copied and then passed to the function. So any modifications of `x` in the function body do not affect the variable `x` from the caller's perspective.
```{r}
a <- c(1, 2)
foo <- function(x) {
x[1] <- 3
x
}
foo(a)
a
```
This is also true for assignments. If a variable `y` is assigned to take the same value that `x` takes, modifications of `y` do not affect `x`.
```{r}
x <- c(1, 2)
y <- x
y[1] <- 3
y
x
```
In the above, we have said that it is *as if* the values are copied. There are performance enhancements to make this less computationally costly in some situations, and R actually uses a [copy on modify](https://adv-r.hadley.nz/names-values.html#copy-on-modify) strategy in the background. This is not necessary to understand to write working code, but knowledge of how R works internally can be used to improve performance.
## Iteration: while and for loops
Iteration allows a program to execute a block of code many times. Iteration (or recursion) greatly increases the expressive power of a programming language.
One way to do this is using a `while` loop, which executes a block of code until some condition is met.
```{r}
x <- 0
while (x < 3) {
x <- x + 1
print(x)
}
```
Another way is to use a `for` loop, where one executes a block of code for each element in a vector.
```{r}
vec <- c(1,4,9)
for (x in vec) {
sqrt.x <- sqrt(x)
print(sqrt.x)
}
```
It is quite common for the vector in a for loop to be defined as `1:n` for some positive integer `n`.
```{r}
vec <- c(1,4,9)
n <- length(vec)
for (i in 1:n) {
print(sqrt(vec[i]))
}
```
Some people prefer to use loop replacements such as `apply` and `lapply`. We will consider this in more detail later.
When using loops, the commands `break` and `next` are useful. The former immediately exits the loop, while the latter immediately starts the next iteration of the loop.
```{r}
x <- 1
while (TRUE) {
x <- x+1
if (x >= 5) {
break
}
}
x
```
```{r}
for (i in 1:3) {
if (i == 2) {
next
}
print(i)
}
```
## Recursion
In some cases, defining a function recursively is natural, and R supports recursive functions. A classic example is a recursive function for computing a Fibonacci number.
```{r}
fib <- function(n) {
stopifnot(n %% 1 == 0 && n >= 0)
if (n == 0 || n == 1) {
return(n)
}
return(fib(n-2) + fib(n-1))
}
# compute and store the first 5 Fibonacci numbers
fib.first5 <- rep(0, 5)
for (i in 1:5) {
fib.first5[i] <- fib(i)
}
fib.first5
```
The recursive function/algorithm `fib` is very poor from a performance perspective. It is good to think about why, and how one might overcome the problem.
## Errors, warnings and messages
You might have noticed the use of the `stopifnot` function in `fib`, which is used there to check that the input is a non-negative integer. It will raise an error if this check fails. Checks like this are useful for ensuring that functions are used only in appropriate situations.
In general, an error can be *thrown* using the `stop` command, which will stop execution and print the error message, unless the error is *caught*.
```
stop("informative error message")
```
```
## Error: informative error message
```
A warning can be printed using the `warning` command, and a message can be printed using the `message` command. Neither warnings and messages stop execution, and the difference between them is primarily semantic: warnings are often used to indicate something unexpected has occurred, but the function was probably able to deal with it appropriately. The messages from `stop`, `warning` and `message` are sent to [standard error and not standard out](https://en.wikipedia.org/wiki/Standard_streams), in contrast to `print`.
```{r}
warning("informative warning message")
message("informative message")
```
Errors, warnings and messages are collectively known as *conditions*.
One can *catch* a condition using `tryCatch`. `error`, `warning`, `message` and `finally` are named arguments, the first three being functions that are called when the corresponding condition is raised. `finally` is just a block of code.
```{r}
tryCatch({
warning("be careful!")
}, error = function(e) {
paste("there was an error:", e$message)
}, warning = function(w) {
paste("there was a warning:", w$message)
}, message = function(m) {
paste("there was a message:", m$message)
}, finally = {
print("finished.")
})
```
If all you want to do is display when there is an error you can use `try`, which allows execution to continue and displays the error message.
```{r}
try(stop("something is wrong"))
```
## Special values
R has some special values, including `NULL`, `NA`, `Inf` and `NaN`.
`NULL` is returned by functions with no return value, and can be thought of us an empty vector.
```{r}
x <- NULL
x <- c(x, 1)
x <- c(x, 2)
x
```
`NA` is "Not Available" and is used for missing values. `NA` is propagated where appropriate.
```{r}
x <- c(1, NA)
y <- c(3, 4)
x + y
```
`Inf` represents positive infinity. `-Inf` represents minus infinity.
`NaN` is "Not a Number" and is the value of, e.g., `0/0`, `Inf - Inf` or `Inf/Inf`.