-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment2_Perreault-graded.Rmd
255 lines (199 loc) · 6.01 KB
/
assignment2_Perreault-graded.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
---
title: 'Bios 6301: Assignment 2'
author: 'Andrea Perreault'
output: pdf_document
---
**Grade: 48/50**
*(informally) Due Tuesday, 20 September, 1:00 PM*
50 points total.
This assignment won't be submitted until we've covered Rmarkdown.
Create R chunks for each question and insert your R code appropriately.
Check your output by using the `Knit PDF` button in RStudio.
**QUESTION 1**
**Working with data** In the `datasets` folder on the course GitHub repo, you will find a file called `cancer.csv`, which is a dataset in comma-separated values (csv) format. This is a large cancer incidence dataset that summarizes the incidence of different cancers for various subgroups. (18 points)
1. Load the data set into R and make it a data frame called `cancer.df`. (2 points)
```{r}
cancer.df <- read.csv("https://github.com/fonnesbeck/Bios6301/raw/master/datasets/cancer.csv", header=TRUE)
head(cancer.df)
```
2. Determine the number of rows and columns in the data frame. (2)
```{r}
nrow(cancer.df)
ncol(cancer.df)
```
```
The cancer.df data frame has 42,120 rows and 8 columns.
```
3. Extract the names of the columns in `cancer.df`. (2)
```{r}
names(cancer.df)
```
4. Report the value of the 3000th row in column 6. (2)
```{r}
cancer.df[3000, 6]
```
5. Report the contents of the 172nd row. (2)
```{r}
cancer.df[172,]
```
6. Create a new column that is the incidence *rate* (per 100,000) for each row.(3)
```{r}
cancer.df[,'incidenceRate'] <- cancer.df[,'incidence']/100000
head(cancer.df)
```
**JC Grading - 1**
For incidence rate above should be incidence / population * 100000
7. How many subgroups (rows) have a zero incidence rate? (2)
```{r}
zeroIR <- cancer.df[cancer.df$incidenceRate==0, ]
nrow(zeroIR)
```
```
A total of 23,191 rows have a zero incidence rate.
```
8. Find the subgroup with the highest incidence rate.(3)
```{r}
maxIR <- cancer.df[which.max(cancer.df$incidenceRate), ]
maxIR
```
**JC Grading - 1**
syntax is fine but answer is incorrect b/c of how incidence rate was calculated
**QUESTION 2**
**Data types** (10 points)
1. Create the following vector: `x <- c("5","12","7")`. Which of the following commands will produce an error message? For each command, Either explain why they should be errors, or explain the non-erroneous result. (4 points)
max(x)
sort(x)
sum(x)
```{r}
x <- c("5","12","7")
```
```
The command max(x) returns "7", which is not the max numeric number in the vector.
But it's place in the vector is the max location.
The command sort(x) returns "12" "5" "7". This order is not increasing numerically because
of the 1 character in the 12 element.
The command sum(x) returns an error because the entries in the vector
are characters, not numbers.
```
2. For the next two commands, either explain their results, or why they should produce errors. (3 points)
y <- c("5",7,12)
y[2] + y[3]
```
The first command 'y <- c("5",7,12)' results in a vector with the entries of 5, 7, and 12.
There are no displayed errors, but it's important to see that the elements are characters.
When attempting to add elements with the command 'y[2] + y[3]', the output is a non-numeric
argument error. Since the first entry in the vector is a character, the others are coerced
into this data type. Therefore, you cannot add numbers in charater data type.
```
3. For the next two commands, either explain their results, or why they should produce errors. (3 points)
z <- data.frame(z1="5",z2=7,z3=12)
z[1,2] + z[1,3]
```
The first command 'z <- data.frame(z1="5",z2=7,z3=12)' results in a data frame with 3 entries.
Even though the first element is added as a character, it is stored as a numeric data type.
The second command 'z[1,2] + z[1,3]' takes the element in the first row, second column and adds
it to the element in the first row, third column. This results in 19, which is the sum of 7 and 12.
```
**QUESTION 3**
**Data structures** Give R expressions that return the following matrices and vectors (*i.e.* do not construct them manually). (3 points each, 12 total)
1. $(1,2,3,4,5,6,7,8,7,6,5,4,3,2,1)$
```{r}
a <- rep(seq(1,8))
b <- rep(seq(7,1))
c <- c(a,b)
c
```
2. $(1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)$
```{r}
d <- rep(1:5, times=1:5)
d
```
3. $\begin{pmatrix}
0 & 1 & 1 \\
1 & 0 & 1 \\
1 & 1 & 0 \\
\end{pmatrix}$
```{r}
m <- matrix(0,nrow=3, ncol=3)
m[upper.tri(m, diag=FALSE)] <- 1
m[lower.tri(m, diag=FALSE)] <- 1
m
```
4. $\begin{pmatrix}
1 & 2 & 3 & 4 \\
1 & 4 & 9 & 16 \\
1 & 8 & 27 & 64 \\
1 & 16 & 81 & 256 \\
1 & 32 & 243 & 1024 \\
\end{pmatrix}$
```{r}
me <- matrix(0, nrow=5, ncol=4)
r1 <- t(rep(seq(1:4)))
me[1,] <- r1
for (i in seq_along(2:6)){
me[i,] = (me[1,])^i
}
me
```
**QUESTION 4**
**Basic programming** (10 points)
1. Let $h(x,n)=1+x+x^2+\ldots+x^n = \sum_{i=0}^n x^i$. Write an R program to calculate $h(x,n)$ using a `for` loop. (5 points)
```{r}
n = 10 # change this value as desired
x = 3 # change this value as desired
x.vec <- rep(0,n)
x.vec[1] = 1
for (i in 2:n) {
x.vec[i] = x^(i-1)
}
x.vec
sum(x.vec)
```
2. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Write an R program to perform the following calculations. (5 points)
```{r}
xa <- seq(1:10)
multa <- c()
for (i in xa-1) {
if(i%%3==0) {
multa <- c(multa, xa[i])
} else {
if(i%%5==0) {
multa <- c(multa, xa[i])
}
}
}
print(sum(multa))
```
3. Find the sum of all the multiples of 3 or 5 below 1,000. (3, [euler1])
```{r}
xb <- seq(1:1000)
multb <- c()
for (i in xb-1) {
if(i%%3==0) {
multb <- c(multb, xb[i])
} else {
if(i%%5==0) {
multb <- c(multb, xb[i])
}
}
}
print(sum(multb))
```
4. Find the sum of all the multiples of 4 or 7 below 1,000,000. (2)
```{r, cache=TRUE}
xc <- seq(1:1000000)
multc <- c()
for (i in xc-1) {
if(i%%4==0) {
multc <- c(multc, xc[i])
} else {
if(i%%7==0) {
multc <- c(multc, xc[i])
}
}
}
sum(as.numeric(multc))
```
Some problems taken or inspired by projecteuler.
[euler1]: https://projecteuler.net/problem=1
[euler2]: https://projecteuler.net/problem=2