forked from swcarpentry/r-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-supp-data-structures.Rmd
402 lines (298 loc) · 9.91 KB
/
01-supp-data-structures.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
---
layout: page
title: Programming with R
subtitle: Data types and structures
minutes: 45
---
> ## Learning Objectives {.objectives}
>
> * Expose learners to the different data types in R
> * Learn how to create vectors of different types
> * Be able to check the type of vector
> * Learn about missing data and other special values
> * Getting familiar with the different data structures (lists, matrices, data frames)
### Understanding Basic Data Types in R
To make the best of the R language, you'll need a strong understanding of the
basic data types and data structures and how to operate on those.
Very important to understand because these are the objects you will manipulate
on a day-to-day basis in R. Dealing with object conversions is one of the most
common sources of frustration for beginners.
**Everything** in R is an object.
R has 6 (although we will not discuss the raw class for this workshop) atomic
vector types.
* character
* numeric (real or decimal)
* integer
* logical
* complex
By *atomic*, we mean the vector only holds data of a single type.
* **character**: `"a"`, `"swc"`
* **numeric**: `2`, `15.5`
* **integer**: `2L` (the `L` tells R to store this as an integer)
* **logical**: `TRUE`, `FALSE`
* **complex**: `1+4i` (complex numbers with real and imaginary parts)
R provides many functions to examine features of vectors and other objects, for
example
* `class()` - what kind of object is it (high-level)?
* `typeof()` - what is the object's data type (low-level)?
* `length()` - how long is it? What about two dimensional objects?
* `attributes()` - does it have any metadata?
```{r}
# Example
x <- "dataset"
typeof(x)
attributes(x)
y <- 1:10
y
typeof(y)
length(y)
z <- as.numeric(y)
z
typeof(z)
```
R has many __data structures__. These include
* atomic vector
* list
* matrix
* data frame
* factors
### Atomic Vectors
A vector is the most common and basic data structure in R and is pretty much the
workhorse of R. Technically, vectors can be one of two types:
* atomic vectors
* lists
although the term "vector" most commonly refers to the atomic types not to lists.
### The Different Vector Modes
A vector is a collection of elements that are most commonly of mode `character`,
`logical`, `integer` or `numeric`.
You can create an empty vector with `vector()`. (By default the mode is
`logical`. You can be more explicit as shown in the examples below.) It is more
common to use direct constructors such as `character()`, `numeric()`, etc.
```{r}
vector() # an empty 'logical' (the default) vector
vector("character", length = 5) # a vector of mode 'character' with 5 elements
character(5) # the same thing, but using the constructor directly
numeric(5) # a numeric vector with 5 elements
logical(5) # a logical vector with 5 elements
```
You can also create vectors by directly specifying their content. R will then
guess the appropriate mode of storage for the vector. For instance:
```{r}
x <- c(1, 2, 3)
```
will create a vector `x` of mode `numeric`. These are the most common kind, and
are treated as double precision real numbers. If you wanted to explicitly create
integers, you need to add an `L` to each element (or *coerce* to the integer
type using `as.integer()`).
```{r}
x1 <- c(1L, 2L, 3L)
```
Using `TRUE` and `FALSE` will create a vector of mode `logical`:
```{r}
y <- c(TRUE, TRUE, FALSE, FALSE)
```
While using quoted text will create a vector of mode `character`:
```{r}
z <- c("Sarah", "Tracy", "Jon")
```
### Examining Vectors
The functions `typeof()`, `length()`, `class()` and `str()` provide useful
information about your vectors and R objects in general.
```{r}
typeof(z)
length(z)
class(z)
str(z)
```
> ## Challenge - Finding commonalities {.challenge}
>
> Do you see a property that's common to all these vectors above?
### Adding Elements
The function `c()` (for combine) can also be used to add elements to a vector.
```{r}
z <- c(z, "Annette")
z
z <- c("Greg", z)
z
```
### Vectors from a Sequence of Numbers
You can create vectors as a sequence of numbers.
```{r}
series <- 1:10
seq(10)
seq(from = 1, to = 10, by = 0.1)
```
### Missing Data
R supports missing data in vectors. They are represented as `NA` (Not Available)
and can be used for all the vector types covered in this lesson:
```{r}
x <- c(0.5, NA, 0.7)
x <- c(TRUE, FALSE, NA)
x <- c("a", NA, "c", "d", "e")
x <- c(1+5i, 2-3i, NA)
```
The function `is.na()` indicates the elements of the vectors that represent
missing data, and the function `anyNA()` returns `TRUE` if the vector contains
any missing values:
```{r}
x <- c("a", NA, "c", "d", NA)
y <- c("a", "b", "c", "d", "e")
is.na(x)
is.na(y)
anyNA(x)
anyNA(y)
```
### Other Special Values
`Inf` is infinity. You can have either positive or negative infinity.
```{r}
1/0
```
`NaN` means Not a Number. It's an undefined value.
```{r}
0/0
```
### What Happens When You Mix Types Inside a Vector?
R will create a resulting vector with a mode that can most easily accommodate
all the elements it contains. This conversion between modes of storage is called
"coercion". When R converts the mode of storage based on its content, it is
referred to as "implicit coercion". For instance, can you guess what the
following do (without running them first)?
```{r}
xx <- c(1.7, "a")
xx <- c(TRUE, 2)
xx <- c("a", TRUE)
```
You can also control how vectors are coerced explicitly using the
`as.<class_name>()` functions:
```{r}
as.numeric("1")
as.character(1:2)
```
### Objects Attributes
Objects can have __attributes__. Attributes are part of the object. These include:
* names
* dimnames
* dim
* class
* attributes (contain metadata)
You can also glean other attribute-like information such as length (works on
vectors and lists) or number of characters (for character strings).
```{r}
length(1:10)
nchar("Software Carpentry")
```
### Matrix
In R matrices are an extension of the numeric or character vectors. They are not
a separate type of object but simply an atomic vector with dimensions; the
number of rows and columns.
```{r}
m <- matrix(nrow = 2, ncol = 2)
m
dim(m)
```
Matrices in R are filled column-wise.
```{r}
m <- matrix(1:6, nrow = 2, ncol = 3)
```
Other ways to construct a matrix
```{r}
m <- 1:10
dim(m) <- c(2, 5)
```
This takes a vector and transforms it into a matrix with 2 rows and 5 columns.
Another way is to bind columns or rows using `cbind()` and `rbind()`.
```{r}
x <- 1:3
y <- 10:12
cbind(x, y)
rbind(x, y)
```
You can also use the `byrow` argument to specify how the matrix is filled. From R's own documentation:
```{r}
mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE)
mdat
```
### List
In R lists act as containers. Unlike atomic vectors, the contents of a list are
not restricted to a single mode and can encompass any mixture of data
types. Lists are sometimes called generic vectors, because the elements of a
list can by of any type of R object, even lists containing further lists. This
property makes them fundamentally different from atomic vectors.
A list is a special type of vector. Each element can be a different type.
Create lists using `list()` or coerce other objects using `as.list()`. An empty
list of the required length can be created using `vector()`
```{r}
x <- list(1, "a", TRUE, 1+4i)
x
x <- vector("list", length = 5) ## empty list
length(x)
x[[1]]
x <- 1:10
x <- as.list(x)
length(x)
```
1. What is the class of `x[1]`?
2. What about `x[[1]]`?
```{r}
xlist <- list(a = "Karthik Ram", b = 1:10, data = head(iris))
xlist
```
1. What is the length of this object? What about its structure?
Lists can be extremely useful inside functions. You can “staple” together lots
of different kinds of results into a single object that a function can return.
A list does not print to the console like a vector. Instead, each element of the
list starts on a new line.
Elements are indexed by double brackets. Single brackets will still return
a(nother) list.
### Data Frame
A data frame is a very important data type in R. It's pretty much the *de facto*
data structure for most tabular data and what we use for statistics.
A data frame is a special type of list where every element of the list has same length.
Data frames can have additional attributes such as `rownames()`, which can be
useful for annotating data, like `subject_id` or `sample_id`. But most of the
time they are not used.
Some additional information on data frames:
* Usually created by `read.csv()` and `read.table()`.
* Can convert to matrix with `data.matrix()` (preferred) or `as.matrix()`
* Coercion will be forced and not always what you expect.
* Can also create with `data.frame()` function.
* Find the number of rows and columns with `nrow(dat)` and `ncol(dat)`, respectively.
* Rownames are usually 1, 2, ..., n.
### Creating Data Frames by Hand
To create data frames by hand:
```{r}
dat <- data.frame(id = letters[1:10], x = 1:10, y = 11:20)
dat
```
> ## Useful data frame functions {.callout}
>
> * `head()` - shown first 6 rows
> * `tail()` - show last 6 rows
> * `dim()` - returns the dimensions
> * `nrow()` - number of rows
> * `ncol()` - number of columns
> * `str()` - structure of each column
> * `names()` - shows the `names` attribute for a data frame, which gives the
>column names.
See that it is actually a special list:
```{r}
is.list(iris)
class(iris)
```
| Dimensions | Homogenous | Heterogeneous |
| ------- | ---- | ---- |
| 1-D | atomic vector | list |
| 2-D | matrix | data frame |
> ## Column Types in Data Frames {.challenge}
>
> Knowing that data frames are lists of lists, can columns be of different type?
>
> What type of structure do you expect on the iris data frame? Hint: Use str()
```{r column-types-DF-answer, eval=FALSE, include=FALSE}
# The Sepal.Length, Sepal.Width, Petal.Length and Petal.Width columns are all
# numeric types, while Species is a Factor.
# Lists can have elements of different types.
# Since a Data Frame is just a special type of list, it can have columns of
# differing type (although, remember that type must be consistent within each column!).
str(iris)
```