-
Notifications
You must be signed in to change notification settings - Fork 1
/
00_01_2_IntroToR_solutions.qmd
221 lines (144 loc) · 6.68 KB
/
00_01_2_IntroToR_solutions.qmd
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
---
title: "P01. Introduction to R, part 2: solutions"
---
**A. read in a data file**
```{r eval = F}
myTBdata <- read.table("TB_stats.txt", header=TRUE)
```
```{r echo = F}
myTBdata <- read.table("data/TB_stats.txt", header=TRUE)
```
In order to run this, your computer need to know where "TB_stats.txt" is. You could download it [here](data/TB_stats.txt).
What does the "header=TRUE" option mean?
Answer: R reads in the first row as the column headers (or, names)
**B. Have a look at the first few lines**
```{r}
# Now let's investigate the data file
head(myTBdata)
```
How many rows can you see? What is the first row?
Answer: 1 row of column names and 6 rows of data
**C. What are the names of the columns?**
```{r}
names(myTBdata)
```
Is this what you expected?
Answer: We would expect there to be 6 elements to correspond to the 6 columns. Each column name is stored as a separate element in a vector.
D. How many rows and columns are there in your data ?
```{r}
dim(myTBdata)
```
What is the first number telling you? And the second?
Answer: `dim(…)` gives you a 2 element vector; the first number is the number of rows, the second number is the number of columns.
**E. How are your data stored?**
```{r}
attributes(myTBdata)
```
What new piece of information have you learned from the 'attributes()' function?
Answer: We now know that our data set is stored as a `data.frame`.
**F. Now take a look at some summary statistics for your data**
```{r}
summary(myTBdata)
```
Let's extract some information from our data
**G. First, Calculate the total number of deaths across all countries.**
The following two methods should give you the same answer
```{r}
total_TB_mortality1 <- sum(myTBdata[,2:3]) # method 1
total_TB_mortality2 <- sum(myTBdata$HIV_pos_TB_mortality + myTBdata$HIV_neg_TB_mortality) # method 2
```
Do you think one method is better than the other?
Answer: While method 1 is certaintly shorter, method 2 is generally better for two reasons: 1) it is easier to read because the column names provide information; 2) it is less liable to cause errors (if you for some reason change the indexing of your columns, method 1 might break).
**H. Now let's check that both methods give the same answer.**
We'll use two ways to check this. First, let's output both answers
```{r}
total_TB_mortality1
total_TB_mortality2
```
Now, let's ask R to check whether they are both equal
```{r}
total_TB_mortality1==total_TB_mortality2
# logical expression which gives TRUE if equal and FALSE if not
```
Why might you prefer to use the second check (using the logical expression) than the first?
Answer: For checking two numbers are the same, it is easy to output two numbers and manually check. However, for larger datasets or for multiple checks, comparison will be much quicker if they are automated.
**I. How different is the TB mortality rate in HIV positive persons in Lesotho compared to Zimbabwe?**
First, let's add "mortality rate" as another column in our data frame
```{r}
myTBdata$Mortality_Per1000 <- 1000 * (myTBdata$HIV_pos_TB_mortality + myTBdata$HIV_neg_TB_mortality)/myTBdata$Population
```
Now subset the dataset to extract the TB mortality rate for both Lesotho and Zimbabwe
```{r}
Lesotho_mortalityrate <- myTBdata[myTBdata$Country=="Lesotho", "Mortality_Per1000"]
Zimbabwe_mortalityrate <- myTBdata[myTBdata$Country=="Zimbabwe", "Mortality_Per1000"]
Relative_Mortality_Rate <- Lesotho_mortalityrate / Zimbabwe_mortalityrate
```
How many times higher is the mortality rate for TB in Lesotho as it is in Zimbabwe?
```{r}
paste("The relative mortality rate is", round(Relative_Mortality_Rate, 2), sep=" ")
```
**J. Finally in this section, let's look at what can go wrong when reading in data files.**
In order to complete this section, you would need to download [readfileexample_1.txt](data/readfileexample_1.txt), [readfileexample_2.txt](data/readfileexample_2.txt), and [readfileexample_3.txt](data/readfileexample_3.txt).
\(a\) There is not an equal number of columns in each of the rows.
```{r eval = F}
readFile_a <- read.table("readfileexample_1.txt", header=TRUE)
```
How do you fix this error? Hint: set missing values in the data file to be 'Not Assigned' by adding them as NA in the original file. Try running this line again with the updated file.
\(b\) The wrong delimiter is used
```{r eval = F}
readFile_b <- read.table("readfileexample_2.txt", header=TRUE)
```
```{r echo = F}
readFile_b <- read.table("data/readfileexample_2.txt", header=TRUE)
print(readFile_b)
```
Is an error given? Check out 'readFile_b' - is it correct?
Answer: No error is given but if you type head(readFile_b), the file has not read properly. The file should be read in as a 5 x 3 data.frame. However, instead it is a 5 x 1
How do you fix this? Ask R for help (?read.table) Which option do you need to specify?
Answer: We must specify the delimiter of the text file
```{r eval = F}
readFile_b <- read.table("readfileexample_2.txt", sep = ",", header=TRUE)
```
```{r echo = F}
readFile_b <- read.table("data/readfileexample_2.txt", sep = ",", header=TRUE)
print(readFile_b)
```
Is there another way of fixing this problem?
Answer: We could use the function `read.csv()` which automatically uses the
```{r eval = F}
readFile_b <- read.csv("readfileexample_2.txt", header=TRUE)
```
```{r echo = F}
readFile_b <- read.csv("data/readfileexample_2.txt", header=TRUE)
print(readFile_b)
```
\(c\) The names are read in as data rows rather than names
```{r eval = F}
readFile_c <- read.csv("readfileexample_2.txt", header=FALSE)
```
```{r echo = F}
readFile_c <- read.csv("data/readfileexample_2.txt", header=FALSE)
print(readFile_c)
```
Is an error given? Check out 'readFile_c' - is it correct?
Answer: No, R has read in the first row of the file as regular data points
Type a new line of code to correct this problem (hint: copy-paste from above and change one of the options)
```{r eval = F}
readFile_c <- read.csv("readfileexample_2.txt", header=TRUE)
```
```{r echo = F}
readFile_c <- read.csv("data/readfileexample_2.txt", header=TRUE)
print(readFile_c)
```
\(d\) One of more of the columns contain different classes
```{r eval = F}
readFile_d <- read.table("readfileexample_3.txt", header=TRUE)
```
```{r echo = F}
readFile_d <- read.table("data/readfileexample_3.txt", header=TRUE)
print(readFile_d)
```
Is an error given? Check out 'readFile_d' - is it correct?
Answer: It appears to look OK. However one of the values has mistakenly been inputted as 'ten' rather than '10'. This means that the column has been saved as non-numeric.
How do you fix this issue? Hint: check the 'class' of the problem column. Try running this line again with an updated file.
Answer: CHANGE THE 5th row, 2nd column to the number 10 in the file and re-run the code