-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleCode.Rmd
671 lines (488 loc) · 14.3 KB
/
ExampleCode.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
---
title: "Example Code"
output:
html_document:
toc: true
toc_float: true
collapsed: false
number_sections: false
toc_depth: 4
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message = FALSE)
```
## Loops
Check R help for on Control Flow `?Control`.
`for(){}`
`for(loop control){do something each iteration}`
```{r, eval=F}
for(iterator in vector){
#do something
}
```
Loop control is defined in between the parentheses. The name of the iterator is placed on the left of `in`(can be assigned any name you want, does not need to be declared in advance). During the execution of the loop, the iterator takes on the values inside the vector which is placed on the right side of `in`. Specifically, the following is happening.
Loop steps:
1. iterator <- vector[1]
2. iterator <- vector[2]
3. iterator <- vector[3]
4. etc.
The loop will automatically stop once it reaches the last item in the vector. The loop can be stopped before that using the `break` command.
```{r}
# Make a loop do something 5 times
# i is the iterator
# 1:5 creates a vector with 5 numbers in it, 1, 2, 3, 4, 5
# the loop will run 5 times, because there are five things to assign to i
for(i in 1:5){
print(5)
}
```
```{r}
# show the value of i each step of the loop
for(i in 1:5){
print(i)
}
```
```{r}
# define the vector to loop over in advance
my_sequence <- 1:5
for(i in my_sequence){
print(i)
}
```
```{r}
# Reminder that i becomes the next value in the vector
# your vector can have any order
my_sequence <- c(1,5,2,3,4)
for(i in my_sequence){
print(i)
}
```
```{r}
# index vector does not need to be numbers
my_things <- c("A","B","C","D")
for(i in my_things){
print(i)
}
```
### Breaking a loop
`break` stops a loop. Used with logical statements to define the conditions necessary to cause the break.
```{r}
for(i in 1:10){
if(i <5){
print(i)
} else{
break
}
}
```
### While loops
While loops run until a logical condition is met. Here there is no iterator, just a logic statement that needs to be met.
This one prints i while i is less than 6. As soon as i becomes "not less than 6", then the loop stops. Critically, inside the loop, the value of i increases each iteration.
```{r}
i <- 1 # create an variable
while (i < 6) {
print(i)
i = i+1 #add one eachs step of the loop
}
```
### Repeat loops
Similar to while, but let's do things until a condition is met.
```{r}
i<-0
repeat{
i<-i+1
print(i)
if(i==5){
break
}
}
```
### Examples
Braces are not needed on one line
```{r}
for(i in 1:5) print(i)
```
Using the value of the iterator to assign in values systematically to another variable.
```{r}
# put 1 into the first five positions of x
x <- c() # create empty vector
for(i in 1:5){
x[i] <- 1 # assign 1 to the ith slot in x
}
x
# put the numbers 1-5 in the first 5 positions of x
x <-c()
for(i in 1:5){
x[i] <- i
}
x
```
Make your own counter, when you need one
```{r}
a <- c(1,4,3,5,7,6,8,2)
odd <- c()
counter <- 0
for(i in a){ # i will the values of a in each position
counter <- counter+1
if(i%%2 != 0){
odd[counter] <- "odd"
} else {
odd[counter] <- "even"
}
}
odd
# An alternative strategy
a <- c(1,4,3,5,7,6,8,2)
odd <- c()
# 1:length(a) creates a sequence from 1 to length
for(i in 1:length(a)){
if(a[i]%%2 != 0){
odd[i] <- "odd"
} else {
odd[i] <- "even"
}
}
odd
```
Nesting loops
```{r}
for(i in 1:5){
for(j in 1:5){
print(c(i,j))
}
}
# example of using nested loops to fill the contents
# of a matrix
my_matrix <- matrix(0,ncol=5,nrow=5)
for(i in 1:5){
for(j in 1:5){
my_matrix[i,j] <- i*j
}
}
my_matrix
```
break exits out of the immediate loop
```{r}
# the inside loop stops when i+j is greater than 5
# the outside loop keeps going
sum_of_i_j <- c()
counter <- 0
for(i in 1:5){
for(j in 1:5){
counter <- counter+1
sum_of_i_j[counter] <- i+j
if(i+j > 5){
break
}
}
}
sum_of_i_j
```
## Logical comparison
Logic statements are used to compare two things, or two sets of things. The output of comparison is a TRUE or FALSE statment. If many things are being compared at once, the output could be many TRUE or FALSE statements for each comparison
### equal to
```{r}
1==1 # is 1 equal to 1?
1==2 # is 1 equal to 2?
c(1,2,3) == c(2,1,3) # compares each element with each element
1 == c(2,1,3)
```
### not equal to
```{r}
1!=1 # is 1 equal to 1?
1!=2 # is 1 equal to 2?
c(1,2,3) != c(2,1,3) # compares each element with each element
1 != c(2,1,3)
```
### Greater than/ less than
```{r}
1 > 1 # is 1 greater than 1?
5 > 1 # is 5 greater than 1?
3 < 2 # is 3 less than 2?
3 < 1 # is 3 less than 1?
c(1,2,3) > c(2,1,3) # ask the question element by element
c(1,2,3) < c(2,1,3)
2 > c(1,2,3) # is greater than each of the numbers
```
### >= <=
Is something greater than or equal to something else
```{r}
1 >= 1 # is 1 greater than 1?
5 >= 1 # is 5 greater than 1?
3 <= 2 # is 3 less than 2?
3 <= 1 # is 3 less than 1?
c(1,2,3) >= c(2,1,3) # ask the question element by element
c(1,2,3) <= c(2,1,3)
2 >= c(1,2,3) # is greater than each of the numbers
```
### AND
The ampersand `&` is used for AND, which allows use to evaluate whether two or more properties are all TRUE.
```{r}
# is 16 divisible by 4 AND 8
16%%4 == 0 & 16%%8 ==0
# is 16 divisible by 4 AND 3
16%%4 == 0 & 16%%3 ==0
# is 16 divisible by 8 and 4 and 2
16%%4 == 0 & 16%%8 ==0 & 16%%2 ==0
```
### OR
The `|` is used for OR, which allows use to evaluate at least one of the properties is TRUE.
```{r}
# is 16 divisible by 4 OR 8
16%%4 == 0 | 16%%8 ==0
# is 16 divisible by 4 OR 3
# it is divisible by 4, so the answer is TRUE
# because at least one of the comparisons is TRUE
16%%4 == 0 | 16%%3 ==0
```
### TRUE FALSE
When R returns values as TRUE or FALSE, it return a logical variable. It also treats TRUE as a 1, and FALSE as a 0. In the example below we see it is possible sum up a logical variable with multiple TRUE and FALSE entries.
```{r}
c(1,2,3) == c(1,2,3)
sum(c(1,2,3) == c(1,2,3))
c(1,2,3) == c(2,1,3)
sum(c(1,2,3) == c(2,1,3))
```
## IF ELSE
A carnival operator needs to check if people are taller than a line to see if they can ride the ride. Every time someone goes through the gate, they run an IF ELSE control structure in their head. IF the person is taller than the line, then they can go on the ride; ELSE (otherwise) the person can not go on the ride.
In other words, IF the situation is X, then do something; ELSE (if the situation is not X), then do something different.
IF and ELSE statements let us specify the conditions when specific actions are taken. Generally, IF and ELSE statements are used inside loops (for, or while, or repeat loops), because at each step or iteration of the loop, we want to check something, and then do something.
Consider this:
```{r}
a <- 1 # define a to be a 1
if(a==1){
print(a) # this is what happens if a==1
} else {
print("A is not 1") # this is what happens if a is not 1
}
a <- 2 # define a to be a 1
if(a==1){
print(a) # this is what happens if a==1
} else {
print("A is not 1") # this is what happens if a is not 1
}
```
Normally we find IF and ELSE in a loop like this:
```{r}
a <- c(1,0,1,0,0,0,1) # make a variable contain 1s and 0s
# write a loop to check each element in the variable
# and do different things depending on the element
for(i in a){
if(i == 1){
print("I'm a 1") # what to do when i is 1
} else {
print("I'm not a 1") # what to do when i is not 1
}
}
```
We can have multiple conditions in our if statements.
```{r}
a <- c(1,2,3,1,2,0,1) # make a variable contain 1s and 0s
# write a loop to check each element in the variable
# and do different things depending on the element
for(i in a){
if(i == 1){
print("I'm a 1") # what to do when i is 1
} else if (i==2){
print("I'm a 2") # what to do when i is 2
} else if (i==3){
print("I'm a 3") # what to do when i is 3
} else {
print("I'm not any of the above") #what to do when none are true
}
}
```
## Functions
This section discusses the syntax for writing custom functions in R.
### function syntax
```{r, eval=FALSE}
function_name <- function(input1,input2){
#code here
return(something)
}
```
### example functions
This function has no input between the `()`. Whenever you run this function, it will simply return whatever is placed inside the `return` statement.
```{r}
# define the function
print_hello_world <- function(){
return(print("hello world"))
}
# use the function
print_hello_world()
```
This function simply takes an input, and then returns the input without modifying it.
```{r}
return_input <- function(input){
return(input)
}
# the variable input is assigned a 1
# then we return(input), which will result in a 1
# because the function internally assigns 1 to the input
return_input(1)
a <- "something"
return_input(a)
```
This function takes an input, then creates an internal variable called temp and assigns input+1. Then the contents of temp is returned. Note there, is no checking of the input, so it will return an erro if you input a character (can't add one to a character in R)
```{r, error=TRUE}
add_one <- function(input){
temp <- input+1
return(temp)
}
add_one(1)
add_one("a")
```
This function adds some input checking. We only add one if the input is a numeric type. Otheriwse, we use `stop()` to return an error message to the console
```{r, error=TRUE}
add_one <- function(input){
if(class(input) == "numeric"){
temp <- input+1
return(temp)
} else {
return(stop("input must be numeric"))
}
}
add_one(1)
add_one("a")
```
A function with three inputs
```{r}
add_multiply <- function(input, x_plus,x_times){
temp <- (input+x_plus)*x_times
return(temp)
}
# input is 1
# x_plus <- 2
# x_times <- 3
# will return (1+2)*3 = 9
add_multiply(1,2,3)
```
## Vectorized approaches
Loops are a common tool for doing something many times. R can accomplish the goal of "doing something many times" without loops, using a vectorized approach.
### Basic examples
Let's take a close look at some very basic differences between using a loop, and using R's vectorized approach
Consider the problem of adding a single number to all of the numbers in a vector.
```{r}
nums <- c(1,2,3,4)
# vectorized approach
# R automatically adds 1 to all of the numbers
nums+1
# loop approach
# much longer to write out
for(i in 1:length(nums)){
nums[i] <- nums[i]+1
}
nums
```
How about adding two vectors together, so we add the first two numbers together, then the second two numbers etc.
```{r}
A <- c(1,2,3,4)
B <- c(1,2,3,4)
# vectorized approach
A+B
# loop approach
the_sum <-c()
for(i in 1:length(A)){
the_sum[i] <- A[i]+B[i]
}
the_sum
```
How about comparing the identity of the elements in two vectors to see if they are the same or not?
```{r}
A <- c("c","e","f","g")
B <- c("d","e","f","g")
#vectorized approach
A==B
# loop approach
compared <-c()
for(i in 1:length(A)){
if(A[i]==B[i]){
compared[i] <- TRUE
} else {
compared[i] <- FALSE
}
}
compared
```
### Replicate
`replicate(n, expr)` allows you to repeat a function many times, and return the answer in a vector
```{r}
# returns 1 randomly sampled number from 1 to 10
sample(1:10,1)
# let's repeat the above 10 times using replicate
replicate(10,sample(1:10,1))
```
The next example shows how to write a function to do something, and then use the function inside replicate to repeat the function many times.
For example, we write a function to run a one-sample t-test on a random sample drawn from a normal distribution
```{r}
ttest_result <- function(){
sample <- rnorm(10,0,1)
t_out <- t.test(sample, mu=0)
return(t_out$statistic)
}
# get 10 t-values from repeating the above 10 times
replicate(10, ttest_result() )
```
### apply family
The `apply` family of functions can be used to "apply" a function across elements of an object. A general overview can be found [here](https://www.datacamp.com/community/tutorials/r-tutorial-apply-family)
Some of the apply functions include: `apply()`, `lapply`, and `sapply`.
### lapply and sapply
Here is part of the definition of lapply from the help file:
lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.
Let's see some examples:
Let's apply a function to each of the elements in a vector. To keep things simple, our function will add 1 to a number
```{r}
some_numbers <- c(1,2,3,4)
add_one <- function(x){
return(x+1)
}
# returns a list, containing the answers
lapply(some_numbers, add_one)
# unlists the list
unlist(lapply(some_numbers,add_one))
# sapply does the unlisting for you
sapply(some_numbers, add_one)
```
An alternative syntax for lapply and sapply let's you define the function you want to apply inside the `lapply` or `sapply` function.
In this case, each element in the vector `some_numbers` will become the `x` value in the function.
```{r}
some_numbers <- c(1,2,3,4)
lapply(some_numbers, FUN = function(x){x+1})
sapply(some_numbers, FUN = function(x){x+1})
```
### apply
The `apply` function can be used on 2-dimensional data, and allows you to apply a function across the rows or the columns of the data.
Let's say you had a 5x5 matrix of random numbers. Let's find the sum of each row
```{r}
random_matrix <- matrix(sample(1:10,25, replace=TRUE),ncol=5)
# applies the sum function to each row
# 1 tells apply to go across rows
apply(random_matrix,1,sum)
```
The sum of each column
```{r}
# applies the sum function to each column
# 2 tells apply to go across columns
apply(random_matrix, 2, sum)
```
Let's say we have a matrix storing 3 samples. Each sample has 10 numbers. Each sample is stored in a column, and each row represents an observation.
```{r}
sample_matrix <- matrix(rnorm(30,0,1),ncol=3)
```
Let's use apply to conduct 10 one-sample t-tests, one for each column. In this example, we can pass the `mu=0` parameter into the `t.test` function. However, we will return the entire ouput of each t-test in a list.
```{r}
apply(sample_matrix,2,t.test, mu=0)
```
What if we wanted to return only the t-values, rather than whole output?
You might try this, but it doesn't work
```{r, error=TRUE}
apply(sample_matrix,2,t.test$statistic, mu=0)
```
So, we write a custom function
```{r}
apply(sample_matrix, 2,
FUN = function(x){
t_out <- t.test(x,mu=0)
return(t_out$statistic)
})
```