-
Notifications
You must be signed in to change notification settings - Fork 10
/
lab_dataframes.Rmd
372 lines (276 loc) · 10.9 KB
/
lab_dataframes.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
---
title: "Matrices, Lists and Dataframes"
subtitle: "R Programming Foundation for Life Scientists"
output:
bookdown::html_document2:
highlight: textmate
toc: true
toc_float:
collapsed: true
smooth_scroll: true
print: false
toc_depth: 4
number_sections: true
df_print: default
code_folding: none
self_contained: false
keep_md: false
encoding: 'UTF-8'
css: "assets/lab.css"
include:
after_body: assets/footer-lab.html
---
```{r,child="assets/header-lab.Rmd"}
```
# Introduction
A data set that have more than one dimension is conceptually hard to
store as a vector. For two-dimensional data set the solution to this
is to instead use matrices or data frames. As with vectors all values
in a matrix has to be of the same type (eg. you can not mix for
example characters and numerics in the same matrix). For data frames
this is not a requirement and different columns can have different
modes, but all columns in a data frame have the same number of
entries. In addition to these R also have objects named lists that can
store any type of data set and are not restricted by types or dimensions.
In this exercise you will learn how to:
- Create and work with matrices, data frames and lists
- Perform basic math operator on matrices
- Use functions to summarize information from data frames
- Extract subsets of data from matrices, data frames and lists
- Create S3 object from a list
# Matrices
The command to create a matrix in R is `matrix()`.
As input it takes a vector of values, the number of
rows and the number of columns.
```{r}
X <- matrix(1:12, nrow = 4, ncol = 3)
X
```
Note that if one only specify the number of rows or columns the it
will infer the size of the matrix automatically using the size of
vector and the option given. The default way of filling the matrix is
column-wise, so the first values from the vector ends up in column 1
of the matrix. If you instead wants to fill the matrix row by row you
can set the byrow flag to TRUE.
```{r}
X <- matrix(1:12, nrow = 4, ncol = 3, byrow = TRUE)
X
```
Subsetting a matrix is done the same way as for vectors, but you have
more than one dimension to work with. So you specify the rows and
column needed.
```{r}
X[1,2]
```
If one wants all values in a column or a row this can be specified by
leaving the other dimension empty, hence this code will print all
values in the second column.
```{r}
X[,2]
```
Note that if the retrieved part of a matrix can be represented as a
vector (eg one of the dimension have the length 1) R will convert it
to a vector otherwise it will still be a matrix.
## Exercise
<i class="fas fa-clipboard-list"></i> Create a matrix containing 1:12 as shown similar to the matrix X above.
1. How do find out the length and the mode of the matrix?
```{r,accordion=TRUE}
mode(X)
length(X)
```
2. Extract all the values in the matrix that is larger than 6.
```{r,accordion=TRUE}
X[X>6]
```
3. Swap the positions of column 1 and 3 in the matrix X
```{r,accordion=TRUE}
X[,c(3,2,1)]
```
4. Add a vector with three zeros as a fifth row to the matrix
```{r,accordion=TRUE}
X.2 <- rbind(X, rep(0, 3))
X.2
```
5. Replace all values the first two columns in your matrix with `NA`.
```{r,accordion=TRUE}
X[,1:2] <- NA
X
```
6. Replace all values in the matrix with 0 and convert it to a vector
```{r,accordion=TRUE}
X[] <- 0
as.vector(X)
```
7. In the the earlier exercises, you created a vector with the names of the type Geno\_a\_1, Geno\_a\_2, Geno\_a\_3, Geno\_b\_1, Geno\_b\_2…, Geno\_s\_3 using vectors. In a previous lecture, a function named `outer()` that generates matrices was mentioned. Try to generate the same vector as before, but this time using `outer()`. This function is very powerful, but can be hard to wrap you head around, so try to follow the logic, perhaps by creating a simple example to start with.
```{r}
letnum <- outer(paste("Geno",letters[1:19], sep = "_"), 1:3, paste, sep = "_")
class(letnum)
sort(as.vector(letnum))
```
8. Create two different 2 by 2 matrices named A and B. A should contain the values 1-4 and B the values 5-8. Try out the following commands and by looking at the results see if you can figure out what is going on.
```
A. A * B
B. A / B
C. A %x% B
D. A + B
E. A - B
F. A == B
```
```{r,accordion=TRUE}
A <- matrix(1:4, ncol = 2, nrow = 2)
B <- matrix(5:8, ncol = 2, nrow = 2)
A
B
A * B
A / B
A %x% B
A + B
A - B
A == B
```
9. Generate a 10 by 10 matrix with random numbers. Add row and column names and calculate mean and median over rows and save these in a new matrix.
```{r,accordion=TRUE}
e <- rnorm(n = 100)
E <- matrix(e, nrow = 10, ncol = 10)
colnames(E) <- LETTERS[1:10]
rownames(E) <- colnames(E)
E.means <- rowMeans(E)
E.medians <- apply(E, MARGIN = 1, median)
E.mm <- rbind(E.means, E.medians)
E.mm
```
# Dataframes
Even though vectors are at the very base of R usage, data frames are central to R as the most common ways to import data into R (`read.table()`) will create a data frame. A data frame consists of a set of equally long vectors. As data frames can contain several different data types the command `str()` is very useful to run on data frames.
```{r}
vector1 <- 1:10
vector2 <- letters[1:10]
vector3 <- rnorm(10, sd = 10)
dfr <- data.frame(vector1, vector2, vector3)
str(dfr)
```
In the above example, we can see that the dataframe **dfr** contains 10 observations for three variables that all have different modes, column 1 is an integer vector, column 2 a vector with **factors** and column 3 a numeric vector. It is noteworthy that the second column is a factor even though we just gave it a character vector.
## Exercise
1. Figure out what is going on with the second column in **dfr** data frame described above and modify the creation of the data frame so that the second column is stored as a character vector rather than a factor. Hint: Check the help for `data.frame` to find an argument that turns off the factor conversion.
```{r,accordion=TRUE}
dfr <- data.frame(vector1, vector2, vector3, stringsAsFactors = FALSE)
str(dfr)
```
2. One can select columns from a data frame using either the name or the position. Use both methods to print the last two columns from the **dfr** data frame.
```{r,accordion=TRUE}
dfr[,2:3]
dfr[,c("vector2", "vector3")]
```
3. Print all letters in the **vector2** column of the data frame where the **vector3** column has a positive value.
```{r,accordion=TRUE}
dfr[dfr$vector3>0,2]
dfr$vector2[dfr$vector3>0]
```
4. Create a new vector combining all columns of **dfr** and separate them by a underscore.
```{r,accordion=TRUE}
paste(dfr$vector1, dfr$vector2, dfr$vector3, sep = "_")
```
5. There is a data frame of car information that comes with the base installation of R. Have a look at this data by typing `mtcars`. How many rows and columns does it have?
```{r,accordion=TRUE}
dim(mtcars)
ncol(mtcars)
nrow(mtcars)
```
6. Re-arrange (shuffle) the row names of this data frame and save as a vector.
```{r,accordion=TRUE}
car.names <- sample(row.names(mtcars))
```
7. Create a data frame containing the vector from the previous question and two vectors with random numbers named random1 and random2.
```{r,accordion=TRUE}
random1 <- rnorm(length(car.names))
random2 <- rnorm(length(car.names))
mtcars2 <- data.frame(car.names, random1, random2)
mtcars2
```
8. Now you have two data frames that both contains information on a set of cars. A collaborator asks you to create a new data frame with all this information combined. Create a merged data frame ensuring that rows match correctly.
```{r,accordion=TRUE}
mt.merged <- merge(mtcars, mtcars2, by.x = "row.names", by.y = "car.names")
mt.merged
```
9. Calculate the mean value for the two columns that you added to the **mtcars** data frame. Check out the function `colMeans()`.
```{r,accordion=TRUE}
colMeans(mtcars2[, c("random1", "random2")])
```
Try to modify so you get the mean by cylinder instead. Check out the function `aggregate()`.
```{r,accordion=TRUE}
aggregate(mtcars2$random1, by=list(mtcars$cyl), FUN=mean)
```
# Lists
The last data structure that we will explore are lists, which is a very flexible structure. Lists can combine different data structures and they do not have to be of equal dimensions or have
other restrictions. The drawback with a flexible structure is that it requires a bit more work to interact with.
The syntax to create a list is similar to creation of the other data structures in R.
```{r}
l <- list(1, 2, 3)
```
As with the data frames the `str()` command is very useful for the sometimes fairly complex lists instances.
```{r}
str(l)
```
This example containing only numeric vector is not very exciting example given the flexibility a list structure offers so let's create a more complex example.
```{r}
vec1 <- letters
vec2 <- 1:4
mat1 <- matrix(1:100, nrow = 5)
df1 <- as.data.frame(cbind(10:1, 91:100))
u.2 <- list(vec1, vec2, mat1, df1, l)
```
As you can see a list can not only contain other data structures, but can also contain other lists.
Looking at the `str()` command reveals much of the details of a list
```{r}
str(u.2)
```
With this more complex object, subsetting is slightly trickier than with more the more homogenous objects we have looked at so far.
To look at the first entry of a list one can use the same syntax as for the simpler structures, but note that this will give you a list of length 1 irrespective of the actual type of data structure found.
```{r}
u.2[1]
str(u.2[1])
```
If one instead wants to extract the list entry as the structure that is stored, one needs to "dig" deeper in the object.
```{r}
u.2[[1]]
str(u.2[[1]])
```
This means that the syntax to extract a specific value from a data structure stored in a list can be daunting. Below we extract the second column of a dataframe stored at position 4 in
the list **u.2**.
```{r}
u.2[[4]][,2]
```
## Exercise
1. Create a list containing 1 character vector, a numeric vector, a character matrix.
```{r,accordion=TRUE}
list.2 <- list(vec1 = c("hi", "ho", "merry", "christmas"),
vec2 = 4:19,
mat1 = matrix(as.character(100:81),nrow = 4))
list.2
```
2. Here is a data frame.
```{r}
dfr <- data.frame(letters, LETTERS, letters == LETTERS)
```
Add this dataframe to the list created above.
```{r,accordion=TRUE}
list.2[[4]] <- dfr
```
3. Remove the the second entry in your list.
```{r,accordion=TRUE}
list.2[-2]
```
4. Create a new list that contain 20 sublists, with each entry holding a numeric vector.
```{r,accordion=TRUE}
vec1 <- rnorm(1000)
list.a <- split(vec1, 1:20)
```
5. How long is your list, and how long are each of the vectors that are part of the list?
```{r,accordion=TRUE}
length(list.a)
lapply(list.a, FUN = "length")
```
6. Figure out what the main differences are between the function `lapply()` and `sapply` are and use both of them with the function summary on your newly created list. What are the pros and cons of the two approaches to calculate the same summary statistics?
```{r,accordion=TRUE}
lapply(X = list.a, FUN = "summary")
sapply(X = list.a, FUN = "summary")
```