This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
introduction-to-r-part-1.Rmd
330 lines (232 loc) · 4.72 KB
/
introduction-to-r-part-1.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
---
title: "Introduction to R Part 1"
output: html_notebook
---
#### R in a nutshell
- Statistical programming environments
- Originally designed and implemented by statisticians
- Widely popular due to its extensive collection of community-contributed packages
- Quickly gaining places among traditional proprietary tools such as SAS and STATA for data analytics
#### Learning Objectives
- Understand basic programming concepts: variables, assignment, functions, loops, conditions
- Understand core R concepts: data loading, data types, data access, libraries
- Understand advanced R concepts: data manipulation, visualization
#### Materials on this notebook is based on two lessons by Software Carpentry and Data Carpentry:
- Introduction to Programming using R
- Data Analysis and Visualization in R for Ecology
# Introduction to Programming using R
## Where am I?
```{r}
getwd()
```
## Variables and assignment
```{r}
read.csv("data/combined.csv")
```
- *variable* : label, name, identifier ...
- *value* : the actual content represented by a *variable*
- *assignment* : the act of assigning a value to a variable
- R's assignment notation: *variable* <- *value*
```{r}
x <- 2
```
```{r}
x
```
```{r}
weight_kg <- 97
```
```{r}
weight_lb <- weight_kg * 2.2
```
```{r}
weight_lb
```
```{r}
weight_kg <- 90
```
```{r}
weight_lb <- weight_kg * 2.2
weight_lb
```
*Read data into a variable:*
```{r}
surveys <- read.csv("data/combined.csv")
```
```{r}
head(surveys)
```
*Header is TRUE or Header is FALSE, that is the question!*
```{r}
surveys <- read.csv("data/combined.csv", header = FALSE)
head(surveys)
```
```{r}
surveys <- read.csv("data/combined.csv")
head(surveys)
```
*How to get help?*
```{r}
?read.csv
```
## Data Types
- Data frames are the *de facto* data structure for R's tabular data, and conceptionally equivalent to an Excel spreadsheet but is more powerful and versatile.
- Matrices (multi-dimensional) and vectors (one dimension) are also available for computational purposes.
- Data frames represents a table whose columns are vectors with same length but possible different data types
```{r}
class(surveys)
```
*Structure of a data frame*
```{r}
str(surveys)
```
```{r}
summary(surveys)
```
*Size of a data frame*
```{r}
dim(surveys)
```
```{r}
nrow(surveys)
```
```{r}
ncol(surveys)
```
*Content of a data frame*
```{r}
head(surveys)
```
```{r}
head(surveys, n=10)
```
```{r}
tail(surveys)
```
```{r}
tail(surveys, n=10)
```
*Names*
```{r}
names(surveys)
```
```{r}
surveys_colnames <- names(surveys)
```
```{r}
surveys_colnames
```
```{r}
surveys_rownames <- rownames(surveys)
str(surveys_rownames)
```
## Data frames access: indexing and subsetting
Similar to an Excel spreadsheet, we can extract specific data from a dataframe via 'coordinates': row/column combinations
Accessing a single element
```{r}
surveys[1,1]
```
```{r}
surveys[1,2]
```
Accessing a block of elements
```{r}
surveys[1:5,2]
```
```{r}
surveys[2,3:7]
```
```{r}
surveys[1:5,3:7]
```
Accessing scattered groups of elements
```{r}
?c
```
```{r}
surveys[c(2:4,6:7),]
```
Excluding data with the `-` notation:
```{r}
surveys[1:5, -3]
```
Accessing columns by names:
```{r}
surveys[1:5,"month"]
```
```{r}
surveys[["month"]][1:5]
```
```{r}
surveys$month[1:5]
```
** Challenge: **
- Create a data frame containing on observations from row 200 to the end of the `surveys` data set
- Create a data frame containing the row that is in the middle of the data frame. Store the content in a variable named `surveys_middle`.
- Combine `nrow` with the `-` notation to reproduce the behavior of `head(surveys)`
```{r}
```
```{r}
```
```{r}
```
## Factors
- Special class, representing categorical data
- Can be ordered or unordered
- Stored as integers with labels (text) associated with these unique integers
- Looked and behave like character vectors but are integers under the hood
- Once created, a `factor` object can only contain a pre-defined set of values, known as *levels*.
- *Levels* are sorted alphabetically by default.
```{r}
str(surveys)
```
```{r}
levels(surveys$sex)
```
```{r}
nlevels(surveys$sex)
```
Converting factors:
```{r}
as.character(surveys$sex)
```
```{r}
f <- factor(c(1990,1983,1977,1998,1990))
```
```{r}
f
```
```{r}
as.numeric(f) #incorrect
```
```{r}
as.numeric(as.character(f)) #works
```
```{r}
as.numeric(levels(f))[f] #recommended
```
Renaming factors:
```{r}
plot(surveys$sex)
```
```{r}
sex <- surveys$sex
```
```{r}
levels(sex)
```
```{r}
levels(sex)[1] <- "missing"
```
```{r}
plot(sex)
```
Using `stringsAsFactors=FALSE`
```{r}
surveys <- read.csv('data/combined.csv', stringsAsFactors = TRUE)
str(surveys)
```
```{r}
surveys <- read.csv('data/combined.csv', stringsAsFactors = FALSE)
str(surveys)
```