This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
02-starting-with-data.Rmd
247 lines (182 loc) · 8.86 KB
/
02-starting-with-data.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
---
layout: topic
title: Starting with data
author: Data Carpentry contributors
minutes: 25
---
```{r, echo=FALSE, purl=FALSE}
knitr::opts_chunk$set(results='hide', fig.path='img/r-lesson-')
```
------------
> ## Learning Objectives
>
> * Load external tabular data from a .csv file into R.
> * Describe what an R data frame is.
> * Summarize the contents of a data frame in R.
> * Manipulate categorical data in R using factors.
------------
# Looking at Metadata
```{r, echo=FALSE, purl=TRUE}
# Looking at metadata
```
We are studying a population of Escherichia coli (designated Ara-3), which were propagated for more than 40,000 generations in a glucose-limited minimal medium. This medium was supplemented with citrate which E. coli cannot metabolize in the aerobic conditions of the experiment. Sequencing of the populations at regular time points reveals that spontaneous citrate-using mutants (Cit+) appeared at around 31,000 generations. This metadata describes information on the Ara-3 clones and the columns represent:
| Column | Description |
|------------------|--------------------------------------------|
| sample | clone name |
| generation | generation when sample frozen |
| clade | based on parsimony-based tree |
| strain | ancestral strain |
| cit | citrate-using mutant status |
| run | Sequence read archive sample ID |
| genome_size | size in Mbp (made up data for this lesson) |
The metadata file required for this lesson can be [downloaded directly here](https://raw.githubusercontent.com/datacarpentry/R-genomics/gh-pages/data/Ecoli_metadata.csv) or [viewed in Github](./data/Ecoli_metadata.csv).
> Tip:
> If you can't find the Ecoli_metadata.csv file, or have lost track of it,
> download the file directly using the R `download.file() function`
```{r, eval=TRUE, purl=FALSE}
download.file("https://raw.githubusercontent.com/datacarpentry/R-genomics/gh-pages/data/Ecoli_metadata.csv", "data/Ecoli_metadata.csv")
```
You are now ready to load the data. We are going to use the R function `read.csv()` to load the data file into memory (as a `data.frame`):
```{r, eval=TRUE, purl=FALSE}
metadata <- read.csv('data/Ecoli_metadata.csv')
```
This statement doesn't produce any output because assignment doesn't display
anything. If we want to check that our data has been loaded, we can print the
variable's value: `metadata`
Alternatively, wrapping an assignment in parentheses will perform the assignment
and display it at the same time.
```{r, eval = TRUE, purl = FALSE}
(metadata <- read.csv('data/Ecoli_metadata.csv'))
```
Wow... that was a lot of output. At least it means the data loaded properly. Let's check the top (the first 6 lines) of this `data.frame` using the function `head()`:
```{r, results='show', purl=FALSE}
head(metadata)
```
We've just done two very useful things.
1. We've read our data in to R, so now we can work with it in R
2. We've created a data frame (with the read.csv command) the
standard way R works with data.
# What are data frames?
`data.frame` is the _de facto_ data structure for most tabular data and what we
use for statistics and plotting.
A `data.frame` is a collection of vectors of identical lengths. Each vector
represents a column, and each vector can be of a different data type (e.g.,
characters, integers, factors). The `str()` function is useful to inspect the
data types of the columns.
A `data.frame` can be created by the functions `read.csv()` or `read.table()`, in
other words, when importing spreadsheets from your hard drive (or the web).
By default, `data.frame` converts (= coerces) columns that contain characters
(i.e., text) into the `factor` data type. Depending on what you want to do with
the data, you may want to keep these columns as `character`. To do so,
`read.csv()` and `read.table()` have an argument called `stringsAsFactors` which
can be set to `FALSE`:
Let's now check the __str__ucture of this `data.frame` in more details with the
function `str()`:
```{r, purl=FALSE}
str(metadata)
```
# Inspecting `data.frame` objects
We already saw how the functions `head()` and `str()` can be useful to check the
content and the structure of a `data.frame`. Here is a non-exhaustive list of
functions to get a sense of the content/structure of the data.
* Size:
+ `dim()` - returns a vector with the number of rows in the first element, and
the number of columns as the second element (the __dim__ensions of the object)
+ `nrow()` - returns the number of rows
+ `ncol()` - returns the number of columns
* Content:
+ `head()` - shows the first 6 rows
+ `tail()` - shows the last 6 rows
* Names:
+ `names()` - returns the column names (synonym of `colnames()` for `data.frame`
objects)
+ `rownames()` - returns the row names
* Summary:
+ `str()` - structure of the object and information about the class, length and
content of each column
+ `summary()` - summary statistics for each column
Note: most of these functions are "generic", they can be used on other types of
objects besides `data.frame`.
### Challenge
Based on the given table of functions to asses data structure, can you answer the following questions?
* What is the class of the object `metadata`?
* How many rows and how many columns are in this object?
* How many citrate+ mutants have been recorded in this population?
As you can see, many of the columns in our data frame are of a special class called
`factor`. Before we learn more about the `data.frame` class, we are going to
talk about factors. They are very useful but not necessarily intuitive, and
therefore require some attention.
## Factors
```{r, echo=FALSE, purl=TRUE}
### Factors
```
Factors are used to represent categorical data. Factors can be ordered or
unordered and are an important class for statistical analysis and for plotting.
Factors are stored as integers, and have labels associated with these unique
integers. While factors look (and often behave) like character vectors, they are
actually integers under the hood, and you need to be careful when treating them
like strings.
In the data frame we just imported, let's do
```{r, purl=TRUE}
str(metadata)
```
We can see the names of the multiple columns. And, we see that
some say things like `Factor w/ 30 levels`
When we read in a file, any column that contains text is automatically
assumed to be a factor. Once created, factors can only contain a pre-defined set values, known as
*levels*. By default, R always sorts *levels* in alphabetical order.
For instance, we see that `cit` is a Factor w/ 3 levels, `minus`, `plus` and `unknown`.
<!--
You can check this by using the function `levels()`, and check the
number of levels using `nlevels()`:
```{r, purl=FALSE, eval=FALSE}
levels(citrate)
nlevels(citrate)
```
Sometimes, the order of the factors does not matter, other times you might want
to specify the order because it is meaningful (e.g., "low", "medium", "high") or
it is required by particular type of analysis. Additionally, specifying the
order of the levels allows to compare levels:
```{r, purl=FALSE, error=TRUE, eval=FALSE}
expression <- factor(c("low", "high", "medium", "high", "low", "medium", "high"))
levels(expression)
expression <- factor(expression, levels=c("low", "medium", "high"))
levels(expression)
min(expression) ## doesn't work
expression <- factor(expression, levels=c("low", "medium", "high"), ordered=TRUE)
levels(expression)
min(expression) ## works!
```
In R's memory, these factors are represented by numbers (1, 2, 3). They are
better than using simple integer labels because factors are self describing:
`"low"`, `"medium"`, and `"high"`" is more descriptive than `1`, `2`, `3`. Which
is low? You wouldn't be able to tell with just integer data. Factors have this
information built in. It is particularly helpful when there are many levels
(like the species in our example data set).
### Converting factors
If you need to convert a factor to a character vector, simply use
`as.character(x)`.
Converting a factor to a numeric vector is however a little trickier, and you
have to go via a character vector. Compare:
```{r, purl=TRUE, eval=FALSE}
f <- factor(c(1, 5, 10, 2))
as.numeric(f) ## wrong! and there is no warning...
as.numeric(as.character(f)) ## works...
as.numeric(levels(f))[f] ## The recommended way.
```
### Challenge
The function `table()` tabulates observations and can be used to create
bar plots quickly. For instance:
```{r wrong-order, results='show', purl=TRUE}
## Question: How can you recreate this plot but by having "control"
## being listed last instead of first?
exprmt <- factor(c("treat1", "treat2", "treat1", "treat3", "treat1", "control",
"treat1", "treat2", "treat3"))
table(exprmt)
barplot(table(exprmt))
```
```{r correct-order, purl=FALSE}
exprmt <- factor(exprmt, levels=c("treat1", "treat2", "treat3", "control"))
barplot(table(exprmt))
```
--->