-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntroduction_R.qmd
668 lines (445 loc) · 16.2 KB
/
Introduction_R.qmd
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
---
title: "Introduction to R"
author: "Elisa Pierfederici and Matthew Good"
date: 2023-03-13
institute: Digital Scholarship Center
format:
revealjs:
theme: simple
css: ["theme/theme.css"]
editor: visual
echo: true
---
## Data science project
![](fig/data-science.png){fig-align="center"}
## The RStudio interface
![](fig/rstudio_session_4pane_layout.png){fig-align="center"}
## Create a project
![](fig/new_project_window.png){fig-align="center"}
## Organizing your working directory
![](fig/directory_organization.png){fig-align="center"}
## Create the first R script
1. Click the **`File`** menu and select **`New File`** and then **`R Script`**
2. Save your script by clicking the **`save/disk icon`** that is in the bar above the first line in the script editor
## Downloading the data
1. Download the dataset called "`SAFI_clean.csv`" <https://ndownloader.figshare.com/files/11492171>
2. Place this downloaded file in the `data folder` you just created.
## Installing packages
![](fig/install_packages.png){fig-align="center"}
## Installing packages
![](fig/install_packages_2.png){fig-align="center"}
## Shortcut - run a code
- PC: `Ctrl` + `Enter`
- Mac: `Cmd`+ `Return`
## Creating an object - **`assignment`**
You can create new objects with `<-`:
```{r}
area_hectares <- 1.0
```
Inspect an object
```{r}
area_hectares
```
## Descriptive names
- Object names must **`start with a letter`**
- Only contain letters, numbers, `_` and `.`
::: {style="background-color: #f1ee8e;"}
Recomanded **`snake_case`** separate lowercase words with `_`
:::
## Descriptive names - examples
```{r}
this_is_a_really_long_name <- 2.5 # numeric
this_is_a_really_long_name
```
```{r}
x <- "hello world" # character
x
```
```{r}
hh_members <- c(3, 7, 10, 6) # vector of numbers
hh_members
```
# Data types & structure {#sec-data-types--structure style="color: darkblue"}
## Types of data {.nostretch}
| Type | Definition | Example |
|------------------|-----------------------------------|-------------------|
| Integer | whole numbers from -inf to +inf | 1L, -2L |
| Numeric/Double | numbers, fractions & decimals from -inf to + inf | 7, 0.2, -5/2 |
| Character/String | quoted strings of letters, numbers, and allowed symbols | "1", "one", "o_n\_e", "hello" |
| Logical | logical constant of True or False | `TRUE`, `FALSE`, `T`, `F` |
## Types of data
You can use **`typeof()`** to find out the type of value or object
```{r}
typeof(1)
```
```{r}
typeof(TRUE)
```
```{r}
typeof("one")
```
## Types of data
| Type | Defintion | Example |
|-----------|---------------------------------------------------|-----------|
| NA | Missing value; technically represented as different types but displayed as `NA` | `NA` |
| NULL | The `NULL` object; an object that exists but is completely empty | `NULL` |
# Data structure {#sec-data-structure style="color: darkblue"}
## Vectors
Often, we are not working with individual values, but with multiple related values ---
::: {style="background-color: #f1ee8e;"}
vector of values!
:::
We can create a vector of ordered numbers using the form **`starting_number:ending_numbers`**
```{r}
x <- 1:5
x
```
Lets look at the Environment pane in RStudio
## Vectors
We can create a vector of any numbers we want using `c()`, which is a **function**.
```{r}
# combine values into a vector and assign to an object names "x"
x <- c(2, 8.5, 1, 9)
# print x
x
```
## Vectors
Vectors are just 1-dimensional sequences **of a single type of data**.
::: {style="background-color: #f1ee8e;"}
Note that vectors can also include strings or character values.
:::
```{r}
letters <- c("a", "b", "c", "d")
letters
```
## Vectors
The general rule R uses is to set the vector to be the most "permissive" type necessary.
For example, what happens if we combine the vectors `x` (from earlier) and `letters` together?
```{r}
mixed_vec <- c(x, letters)
mixed_vec
```
## Vectors
```{r}
mixed_vec
```
Notice the quotes? R turned all of our numbers into strings, since strings are more "permissive" than numbers.
```{r}
typeof(mixed_vec)
```
This is called **coercion**. R coerces a vector into whichever type will accommodate all of the values
## Vectors
We can coerce `mixed_vec` to be numeric using **`as.numeric()`**, notice what happens to the character values
```{r}
as.numeric(mixed_vec)
```
# Help {#sec-help style="color: darkblue"}
## RStudio
```{r}
?as.numeric
```
## Google
![](fig/Stackoverflow.png){fig-align="center"}
## 📝1
::: panel-tabset
### Q1
Create an object called `a` that is just the letter "a" and an object `x` that is assigned the number 8. Add `a` to `x`. What happens?
### Q2
Create a vector called `b` that is just the number 8 in quotes. Add `b` to `x` (from above). What happens?
### Q3
Find some way to add `b` to `x`. (*Hint*: Don't forget about coercion.)
:::
## Solution 1
::: panel-tabset
### Q1
```{r}
a <- "a"
x <- 8
## a + x
```
### Q2
```{r}
b <- "8"
## b + x
```
### Q3
```{r}
as.numeric(b) + x
```
:::
## Indexing vectors
How do we extract elements out of vectors?
This is called **indexing**, and it is frequently quite useful
There are a number of methods for indexing that are good to be familiar with
## **Indexing by position**
Vectors can be indexed numerically, starting with 1 (not 0). We can extract specific elements from a vector by putting the index of their position inside brackets `[]`.
## **Indexing by position**
Let's take a new vector `z` as an example:
```{r}
z <- 6:10
```
Let's get just the first element of `z`:
```{r}
z[1]
```
Get the first and third element by passing those indexes as a vector using `c()`.
```{r}
z[c(1, 3)]
```
## List
While vectors are useful for storing a single type of data, they're not well-suited for storing heterogeneous data. In other words, if we have different types of data we want to store together, we need a different data structure.
## List
For example, let's say we want to store the year we're in a PhD program (a number), our name (a string), and our enrollment status (a logical) in a single object that preserves these different types. In this case, a vector won't work because it can only contain elements of the same type. Instead, we can use a list.
Lists are similar to vectors in that they're 1-dimensional, but they can store heterogeneous data. In other words, each element in a list can be a different type of data. This makes lists a more flexible data structure for storing complex or diverse data.
## Creating Lists
We can create a list with the **`list()`** function
```{r}
brendan <- list(4L, "Brendan Cullen", TRUE)
brendan
```
## Creating Lists
And, we can give each element of the list a name to make it easier to keep track of them.
```{r}
brendan <- list(year = 4L,
name = "Brendan Cullen",
enrollment = TRUE)
brendan
```
Notice that \[\[1\]\], \[\[2\]\], and \[\[3\]\], the element indices, have been replaced by the names year, name, and enrollment 👀
## Creating Lists
You can see also see the names of a list by running **`names()`** on it
```{r}
names(brendan)
```
## Indexing Lists
If we want the actual object stored at the first position instead of a list containing that object, we have to use double-bracket indexing list\[\[i\]\]:
```{r}
brendan[[1]]
```
# Break
## Data frames
A **data frame** is a common way of representing rectangular data \-- collections of values that are each associated with a variable (column) and an observation (row). In other words, it has 2 dimensions.
A data frame is technically a special kind of list \-- it can contain different kinds of data in different columns, but each column must be the same length.
## Data frames
![](fig/Dataframe.png){fig-align="center"}
## Import data frames {style="blue"}
### From *.csv*
1. Upload **`package tidyverse`**
2. Load the library
```{r}
## load the tidyverse
library(tidyverse)
library(here)
```
## Import data frames
### From *.csv*
3. Import data frame and call it interview
```{r}
interviews <- read_csv(here("data", "SAFI_clean.csv"), na = "NULL")
```
4. Inspect database
```{r}
#| eval: false
## inspect the data
interviews
## view(interviews)
## head(interviews)
```
## **Presentation of the SAFI Data**
SAFI (Studying African Farmer-Led Irrigation) is a study looking at farming and irrigation methods in Tanzania and Mozambique. The survey data was collected through interviews conducted between November 2016 and June 2017. For this lesson, we will be using a subset of the available data. For information about the full teaching dataset used in other lessons in this workshop, see the [dataset description](https://www.datacarpentry.org/socialsci-workshop/data/).
## Inspecting data frames
### Size
- **`dim(interviews)`** - returns a vector with the number of rows as the first element, and the number of columns as the second element (the dimensions of the object)
- **`nrow(interviews)`** - returns the number of rows **`ncol(interviews)`** - returns the number of columns
## Inspecting data frames
### Content:
- **`head(interviews)`** - shows the first 6 rows **`tail(interviews)`** - shows the last 6 rows
## Inspecting data frames
### Summary:
- **`str(interviews)`** - structure of the object and information about the class, length and content of each column
- **`summary(interviews)`** - summary statistics for each column
## Subsetting data frames
Let's get the first row and third column of `interviews` using numerical indexing
```{r}
## first element in the first column
interviews[1, 1]
```
```{r}
## first element in the 6th column of the tibble
interviews[1, 6]
```
## Subsetting data frames
```{r}
## first three elements in the 7th column of the tibble
interviews[1:3, 7]
```
```{r}
## equivalent to head_interviews <- head(interviews)
head_interviews <- interviews[1:6, ]
```
## Negative Subsetting data frames
```{r}
# The whole tibble, except the first column
interviews[, -1]
```
## 📝2
::: panel-tabset
### Q1
Create a tibble (interviews_100) containing only the data in row 100 of the interviews dataset.
### Q2
Notice how nrow() gave you the number of rows in the tibble?
- Use that number to pull out just that last row in the tibble.
- Compare that with what you see as the last row using tail() to make sure it's meeting expectations.
:::
## Solution 2
::: panel-tabset
### Q1
```{r}
## 1.
interviews_100 <- interviews[100, ]
interviews_100
```
### Q2
```{r}
## 2.
# Saving `n_rows` to improve readability and reduce duplication
n_rows <- nrow(interviews)
interviews_last <- interviews[n_rows, ]
```
:::
# Grammar of data manipulation
## Learning **`dplyr`**
::: {style="background-color: #f1ee8e;"}
`select()` picks variables based on their names.\
`filter()` picks cases based on their values.
:::
`mutate()` - adds or alters variables that are functions of existing variables\
`summarise()` reduces multiple values down to a single summary.\
`arrange()` changes the ordering of the rows.
## `filter()` - subsetting rows
![](gif/filtering.gif){fig-align="center"}
#
## `select()` - reduce columns
Reducing the number of columns (or rearranging columns)
![](gif/selecting.gif){fig-align="center"}
## Select columns
```{r}
#| eval: false
# to select columns throughout the dataframe
select(interviews, village, no_membrs, months_lack_food)
# to do the same thing with subsetting
interviews[c("village","no_membrs","months_lack_food")]
# to select a series of connected columns
select(interviews, village:respondent_wall_type)
```
## Filtering rows
```{r}
#| eval: false
# filters observations where village name is "Chirodzo"
filter(interviews, village == "Chirodzo")
```
## Filtering rows
```{r}
#| eval: false
# filters observations with "and" operator (comma)
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo",
rooms > 1,
no_meals > 2)
```
## Filtering rows
```{r}
#| eval: false
# filters observations with "&" logical operator
# output dataframe satisfies ALL specified conditions
filter(interviews, village == "Chirodzo" &
rooms > 1 &
no_meals > 2)
```
## Shortcut - the pipe
- PC: `Ctrl` + `Shift` + `M`
- Mac: `Cmd` + `Shift` + `M`
## The pipe
```{r}
#| eval: false
# standard
interviews2 <- filter(interviews, village == "Chirodzo")
interviews_ch <- select(interviews2, village:respondent_wall_type)
# piped
interviews_ch <- interviews %>%
filter(village == "Chirodzo") %>%
select(village:respondent_wall_type)
interviews_ch
```
## 📝3
Using pipes, subset the interviews data to include interviews where respondents were members of an irrigation association (memb_assoc) and retain only the columns affect_conflicts, liv_count, and no_meals.
## Solution 3
```{r}
#| eval: false
interviews %>%
filter(memb_assoc == "yes") %>%
select(affect_conflicts, liv_count, no_meals)
```
## **`Mutate()`** - add column
create new columns based on the values in existing columns
Example: we are interest in the avg number of people per room
```{r}
#| eval: false
interviews %>%
mutate(people_per_room = no_membrs / rooms)
```
## **`Mutate()`** - add column
We may be interested in investigating whether being a member of an irrigation association had any effect on the ratio of household members to rooms. To look at this relationship, we will first remove data from our dataset where the respondent didn't answer the question of whether they were a member of an irrigation association. These cases are recorded as "NULL" in the dataset.
```{r}
#| eval: false
interviews %>%
filter(!is.na(memb_assoc)) %>%
mutate(people_per_room = no_membrs / rooms)
```
## **`Mutate()`** - add column
- `is.na()` returns a value of `TRUE` (because the memb_assoc is missing)
- the `!`symbol negates this and says we only want values of `FALSE`, where memb_assoc is not missing.
##
📝4
Create a new dataframe from the `interviews` data that meets the following criteria: contains only the `village` column and a new column called `total_meals` containing a value that is equal to the total number of meals served in the household per day on average (`no_membrs` times `no_meals`). Only the rows where `total_meals` is greater than 20 should be shown in the final dataframe.
**Hint**: think about how the commands should be ordered to produce this data frame!
##
Solution 4
```{r}
#| eval: false
interviews_total_meals <- interviews %>%
mutate(total_meals = no_membrs * no_meals) %>%
filter(total_meals > 20) %>%
select(village, total_meals)
```
## Count() - counting
If we wanted to count the number of rows of data for each village
```{r}
interviews %>%
count(village)
```
```{r}
interviews %>%
count(village, sort = TRUE) # to get results in decreasing order
```
## Summarize() - summary statistic
- `group_by()`collapses each group into a single-row summary of that group.
```{r}
#| eval: false
interviews %>%
group_by(village)
```
## Summarize() - summary statistic
- takes as arguments the column names that contain the **categorical** variables for which you want to calculate the summary statistics.
```{r}
#| eval: false
interviews %>%
group_by(village) %>%
summarize(mean_no_membrs = mean(no_membrs))
```
```{r}
#| eval: false
interviews %>%
group_by(village, memb_assoc) %>% # group by multiple col
summarize(mean_no_membrs = mean(no_membrs))
```