-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathR_functions_and_loops.Rmd
156 lines (101 loc) · 3.38 KB
/
R_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
153
154
155
156
---
title: 'R: Loops and Functions'
author: "Murray Cadzow"
date: "9 November 2015"
output: html_document
---
https://github.com/murraycadzow/R_tutorials/
# Lesson 7:
This lesson will cover the following:
- Functions
- Control Statements
- Loops
## Functions
As you have been using R you will have noticed that many tasks have a particular function already available for you to use, such as mean or sd. In this section we are going to learn how to make our own functions
We can define our own functions using the *function()* function
Inside the parenthesis we define what variables are going to be passed to our function and curly braces contain the body of the function. If we want to return a value from our function we end it with *return()*
We can assign this new function to a variable
To call our new function we now use the variable name and pass any required arguments
```{r, eval = TRUE, echo = TRUE}
double <- function( num ){
num <- num * 2
return( num )
}
double(2)
```
We can also have multiple arguments
```{r, eval = TRUE, echo = TRUE}
calcBMI <- function(height, weight){
return( weight / height ^2)
}
calcBMI( height = 1.68, weight = 73)
h = c(1.68, 1.74, 1.55)
w = c(73, 85, 65)
calcBMI(height = h, weight = w)
```
**NB:** Variables declared only inside a function don't exist outside of the function
## Control Statements
Control statements allow us to use conditions to execute particular pieces of code based on the input
We use the statements **if** or **else** to control what happens, else is only used after an if
```{r}
bmi <- 21
if(bmi > 30){
print("obese")
} else if (bmi > 25){
print("overweight")
} else if (bmi > 20){
print("healthy")
} else {
print("underweight")
}
```
There is also the *ifelse()* function that can be used
it takes the format ifelse( condition, if true, if false)
## Loops
Loops enable us to do the same operation many times until the termination condition is met
The most common loop is the **for** loop
Each iteration of the loop the continuation condition is evaluated
```{r, eval = TRUE, echo = TRUE}
for( num in 1:5){
print( num )
}
```
### For loop using indices
```{r, echo = TRUE, eval = TRUE}
myNumbers <- c(11,12,13,14,15)
for( i in 1:length(myNumbers) ){
print(paste("number =", myNumbers[i], "index =", i))
}
```
### While loop
```{r, echo = TRUE, eval = TRUE}
i = 5
while( i > 0){
print(i)
i <- i - 1
}
```
NB: **always make sure your condition will eventually be FALSE before running**
### Things can get quite fancy
```{r, echo = TRUE, eval = TRUE, message = FALSE}
mydata = read.delim(file = "Tanya_Data.txt", header = TRUE, sep = "\t", stringsAsFactors=FALSE)
ethgroup <- levels(as.factor(mydata$ETHCLASS))
ethgroup
model_coef <-data.frame()
model_confint <- list()
for( eth in ethgroup){
model = glm(formula = GOUTAFFSTAT ~ BMI + AGE + SNP, data = mydata, subset = ETHCLASS == eth)
#insert coefficients into dataframe
model_coef <- rbind(model_coef, exp(coef(model) ))
#make row name be eth for this iteration
rownames(model_coef)[which(ethgroup == eth)] <- eth
#make names of the coefficients be names of columns
names(model_coef) <- names(exp(coef(model)))
#put confidence intervals into a list labelled by eth
model_confint[[eth]] <- exp(confint(model) )
}
model_coef
model_confint
```
### Look for where you repeat yourself and ask:
## **Can I replace this with a function/loop?**