Skip to content

Commit fa989de

Browse files
committed
Minor cleanup to second day of aggregation and joins material
1 parent a917d17 commit fa989de

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

materials/converting-dataframes-vectors.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ language: R
88
### Setup
99

1010
```r
11-
install.packages('dplyr')
11+
install.packages(c('dplyr', 'readr', 'tidyr'))
1212
download.file("https://ndownloader.figshare.com/files/2292172", "surveys.csv")
1313
download.file("https://ndownloader.figshare.com/files/3299474", "plots.csv")
1414
download.file("https://ndownloader.figshare.com/files/3299483", "species.csv")
@@ -39,13 +39,13 @@ surveys <- read_csv("species.csv")
3939
* We do this using `[]`
4040
* Remember that `[]` also mean "give me a piece of something" in R
4141
* Let's get the `species_id` column
42-
* `"species_id"` has to be in quotes because we we aren't using the tidyverse
42+
* `"species_id"` has to be in quotes because we aren't using the tidyverse
4343

4444
```r
4545
species["species_id"]
4646
```
4747

48-
* This actually returns a one column data frame, not a vector
48+
* This actually returns a one column data frame, not a vector, it works like `select`
4949
* To extract a single column as a vector we use two sets of `[]`
5050
* Think of the second set of `[]` as getting the single vector from inside the one column data frame
5151

@@ -57,7 +57,7 @@ species[["species_id"]]
5757
* Shorthand for `[[]]` in cases where the piece of something we want to get has a name
5858
* So, we start with the object we want a part of, our `surveys` data frame
5959
* Then the `$` with no spaces around it
60-
* and then the name of the `species_id` column (without quotes, just to be confusing)
60+
* Then the name of the `species_id` column (without quotes, just to be confusing)
6161

6262
```r
6363
species$species_id
@@ -88,18 +88,18 @@ species |>
8888
* Just like `mutate` and `summarize`
8989

9090
```r
91-
states <- c("FL", "FL", "GA", "SC")
91+
state <- c("FL", "FL", "GA", "SC")
9292
count <- c(9, 16, 3, 10)
9393
area <- c(3, 5, 1.9, 2.7)
94-
count_data <- data.frame(states = states, counts = count, regional_area = area)
94+
count_data <- data.frame(states = state, counts = count, areas = area)
9595
```
9696

9797
* To make a tibble instead of a data.frame use `tibble()`
9898

9999
```r
100100
library(dplyr)
101101

102-
count_data <- tibble(states = states, counts = count, regional_area = area)
102+
count_data <- tibble(states = state, counts = count, areas = area)
103103
```
104104

105105
* `tibble()` is part of the `tibble` package, which gets loaded by `dplyr`
@@ -110,12 +110,17 @@ count_data <- tibble(states = states, counts = count, regional_area = area)
110110
* For example, if all of this data was collected in the same year and we wanted to add that year as a column in our data frame we could do it like this
111111

112112
```r
113-
count_data_year <- tibble(year = 2022, states = states, counts = count, regional_area = area)
113+
count_data_year <- tibble(year = 2022, states = state, counts = count, areas = area)
114114
```
115115

116116
* `year =` sets the name of the column in the data frame
117-
* And `2000` is that value that will occur on every row of that column
118-
* If we run this and look at the `count_data_year` data frame we'll see that it includes the year column with `2000` in every row
117+
* And `2022` is the value that will occur on every row of that column
118+
* _Show `count_data_year` data frame with the year column with `2022` in every row_
119+
120+
> Do [Building data frames from vectors]({{ site.baseurl }}/exercises/building-data-frames-from-vectors-R/).
121+
122+
123+
## Adding columns to existing data frames
119124

120125
* We can add a vector as a new column to an existing data frame using `mutate()`
121126

@@ -125,12 +130,19 @@ elevation <- c(100, 65, 226, 152)
125130
count_data_year_elev <- mutate(count_data_year, elevations = elevation)
126131
```
127132

133+
* We can also do the same thing with `$` or `[]`
134+
135+
```r
136+
count_data_year$elevations <- elevation
137+
count_data_year["elevations"] <- elevation
138+
```
139+
140+
* Note that these changes are actually "in place"
141+
* Unlike everything else
142+
128143
### Summary
129144

130145
* So, that's the basic idea behind how vectors and data frames are related and how to convert between them.
131146
* A data frame is a set of equal length vectors
132147
* We can extract a column of a data frame into a vector using either `$` or two sets of `[]`
133148
* We can combine vectors into data frames using the `data.frame` function, which takes a series of arguments, one vector for each column we want to create in the data frame.
134-
135-
136-
> Do [Building data frames from vectors]({{ site.baseurl }}/exercises/building-data-frames-from-vectors-R/).

materials/dplyr-joins.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ time: 30
99
### Setup
1010

1111
```r
12-
install.packages('dplyr')
12+
install.packages(c('dplyr', 'readr', 'tidyr')
1313
download.file("https://ndownloader.figshare.com/files/2292172", "surveys.csv")
1414
download.file("https://ndownloader.figshare.com/files/3299474", "plots.csv")
1515
download.file("https://ndownloader.figshare.com/files/3299483", "species.csv")
@@ -63,8 +63,8 @@ plots <- read_csv("plots.csv")
6363
combined <- inner_join(surveys, species, join_by(species_id))
6464
```
6565

66-
* Looking at the `combined` table, we can see that on every row with a particular value for `species_id` the join has added the matching values on `genus`, `species`, and `taxa`
67-
* So one way to think about this join is that it adds the relevant information in the `species` table to the `surveys` table
66+
* Looking at the `combined` table, we can see that on every row with a particular value for `species_id` the join has added the matching values for `genus`, `species`, and `taxa`
67+
* One way to think about this join is that it adds the relevant information in the `species` table to the `surveys` table
6868
* Often for scientific data we can think about there being one main table, the `surveys` table in our case, and multiple supplementary tables that provide additional details
6969

7070
* Inner joins keep information from both tables when both tables have a matching value in the join column

0 commit comments

Comments
 (0)