-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession 3 - Functions and loops.Rmd
160 lines (108 loc) · 3.55 KB
/
Session 3 - Functions and loops.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
---
title: "Conditionals, Functions, and Loops"
author: "Ashwin Malshé"
date: "06/01/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Conditionals
Like most other programming languages, R also has `if()` and `else()` statements. Let's look into a simple example.
Generate a random number from a uniform random distribution. If the number is less than 0.5, print `Tails` otherwise print `Heads`
```{r}
a <- runif(1)
print (a)
if ( a < 0.5) {
print("Tails")
} else (print("Heads"))
```
R also has a vectorized `ifelse()`. This can speed up operations. For example, consider a vector A with 10 random numbers between 0 and 1. We want to create a character vector such that if a number in A is less than 0.5, we return "Tails", otherwise "Heads".
With `if` and `else`, we will have to evaluate this separately for each element of A. `ifelse` can do it in one shot.
```{r}
A <- runif(10)
A
B <- ifelse(A < 0.5, "Tails", "Heads")
B
```
*This looks like the formula in Excel!*
## Functions
Functions wrap longer procedures in one single command
Next we will create a function called forever21.
```{r}
forever21 <- function(age) {
if (age <= 21) {
return(age)
} else {
return(21)
}
}
```
```{r}
forever21(50)
forever21(15)
```
Write a function to add two numbers
```{r}
mySum <- function(a, b) {
return(a + b)
}
mySum(5, 10)
mySum(255, 255)
```
Write a function to concatenate two character variables with three delimiters.
For example, you input "Hello" and "world!" separately and you get "Hello world!" In this case the delimiter is space.
```{r}
myWord <- function(a, b) {
w1 <- paste(a, b, sep = "|")
w2 <- paste(a, b, sep = "-")
w3 <- paste(a, b, sep = "***")
return(c(w1, w2, w3))
}
myWord("Hello", "world!")
```
Write a function to winsorize a vector at a given percentage. Winsorization is a common data science technique to mitigate the effect of outliers. Rather than deleting these observations, we can set them to a a set percentile. For instance, if we want to winsorize a variable at 90%, we will set all the values lower than bottom 5% to the 5th percentile and all the values higher than top 5% to the 95th percentile. Thus, we keep middle 90% unchanged.
```{r}
winsor <- function(vec, w = 0.95) {
l1 <- (1 - w) / 2
l2 <- 1 - l1
q <- quantile(vec, c(l1, l2), type = 1)
vec1 <- ifelse(vec < q[1], q[1], vec)
vec2 <- ifelse(vec1 > q[2], q[2], vec1)
return(vec2)
}
```
```{r}
winsor(vec = c(1:100), w = 0.9)
```
## Writing functions with default values
Write a function for area of a circle with radius r. The default value should be for radius r = 1
```{r}
cirArea <- function(r = 1) pi * r^2
```
get area of a circle with radius = 5. Verify the result
```{r}
# Answer using the function
cirArea(5)
# Answer using the raw formula
pi * 5^2
identical(cirArea(5), pi * 5^2)
```
## Saving and retrieving functions
Copy the function and paste it in an R script. Save the file on your hard disk and make note of the path to that file.
When you want to load the function, use
`source("path name to the function R file")`
## Looping in R
```{r}
for (i in 1:10) {
print(paste("My number is", i))
}
```
Also check out `while` command in R. It can be used when the condition is not easy or possible to write in a `for`. For example, you may want to keep running the loop until specific time (e.g., 5 pm) or until you get a specific number of observations from an operation (e.g., download 5,000 tweets)
```{r}
i <- 1
while(i <= 10) {
print(paste("My number is", i))
i <- i + 1
}
```