Skip to content

Commit 4eb080c

Browse files
committed
breaking down funtion exercise into meaningful steps
1 parent 9f6d0ff commit 4eb080c

File tree

2 files changed

+107
-29
lines changed

2 files changed

+107
-29
lines changed

materials/sections/r-practice-function-cleaning-data.qmd

Lines changed: 101 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ library(dplyr)
3131
library(janitor)
3232
```
3333

34-
3. Load the species table using the following code. This code scrapes a table from a url and uses some cleaning and wrangling functions to get the table into our Environement in the format we want.
34+
3. Load the species table using the following code. This code scrapes a table from a url and uses some cleaning and wrangling functions to get the table into our Environment in the format we want.
3535

3636
```{r}
3737
webpage <- rvest::read_html("https://www.pwrc.usgs.gov/BBL/Bander_Portal/login/speclist.php")
@@ -43,12 +43,14 @@ species <- tbls[[1]] %>%
4343
janitor::clean_names() %>%
4444
select(alpha_code, common_name) %>%
4545
mutate(alpha_code = tolower(alpha_code))
46+
47+
head(species, 3)
4648
```
4749

4850
4. Obtain data from the the Arctic Data Center [Utqiaġvik shorebird breeding ecology study, Utqiaġvik](https://doi.org/10.18739/A23R0PT35). Download the following files:
4951

50-
- `Utqiagvik_adult_shorebird_banding.csv`
51-
- `Utqiagvik_chick_shorebird_banding.csv`
52+
- `Utqiagvik_predator_surveys.csv`
53+
- `Utqiagvik_nest_data.csv`
5254

5355
**Note:** It's up to you on how you want to download and load the data! You can either use the download links (obtain by right-clicking the "Download" button and select "Copy Link Address" for each data entity) or manually download the data and then upload the files to RStudio server.
5456

@@ -66,51 +68,122 @@ This is a handy package that requires a moderate amount of knowledge of `html` t
6668

6769
## Write a function that will translate species codes into common names.
6870

69-
<!--
70-
EDIT FROM HERE
7171

7272
::: callout-note
7373
### Read and explore data
74+
Read in each data file and store the data frame as `shorebird_adult` and `shorebird_chick` accordingly. After reading the data, insert a new chunk or in the console, explore the data using any function we have used during the lessons (eg. `colname()`, `glimpse()`)
7475

75-
fsfsfsf
7676
:::
7777

78+
```{r}
79+
#| code-summary: "Answer"
80+
81+
## When reading from a file in your data folder in your Rpoj
82+
nest_data <- read_csv("data/Utqiagvik_nest_data.csv")
83+
84+
predator_survey <- read_csv("data/ Utqiagvik_predator_surveys.csv")
85+
86+
87+
## When reading using the url
88+
nest_data <- read_csv("https://arcticdata.io/metacat/d1/mn/v2/object/urn%3Auuid%3A982bd2fc-4edf-4da7-96ef-0d11b853102d")
89+
90+
predator_survey <- read_csv("https://arcticdata.io/metacat/d1/mn/v2/object/urn%3Auuid%3A9ffec04c-7e2d-41dd-9e88-b6c2e8c4375e")
91+
92+
## Exploring the data (these functions can also be used to explore nest_data)
93+
94+
colnames(predator_survey)
95+
glimpse(predator_survey)
96+
unique(predator_survey$species)
97+
summary(predator_survey)
98+
99+
```
78100

79101

80102
::: callout-note
81-
## Write functions to clean species code
103+
## How would you translate species codes into common names for one of the data frmes?
104+
105+
Before thinking of how to write a function, first discuss what are you trying to achieve and how would you get there. Write and run the code that would allow you to combine the `species` data frame with the `predator_survey` so that the outcome data frame has the species code and common names.
82106

83-
- **Hint**: The fastest way to do this involves adding a column to the `data.frame`. Your function will have two arguments
84-
- **Optional Extra Challenge**: For a little extra challenge, try to incorporate an `if` statement that looks for `NA` values in the common name field you are adding. What other conditionals might you include to make your function smarter?
107+
**Hint:** `joins`
108+
85109
:::
86110

111+
```{r}
112+
#| code-summary: "Answer"
113+
114+
predator_comm_names <- left_join(predator_survey,
115+
species,
116+
by = c("species" = "alpha_code"))
117+
118+
```
119+
120+
87121

88-
### Visual schematic of data
122+
::: callout-note
123+
## Write a functions to add species common name to any data frame.
124+
How can you generalize the code from the previous question and make it into a function?
125+
126+
The idea is that you can use this function in any data frame that has a `species` column with the Bird Banding Laboratory Species Code.
127+
128+
:::
89129

90-
Make this:
91130

92-
year common_name pred_count
93-
2003 Glaucous Gull 54
94-
2003 Parasitic Jaeger 2
95-
2003 Pomarine Jaeger 6
96-
2004 Glaucous Gull 69
97-
2004 Long-tailed Jaeger 13
131+
```{r}
132+
#| code-summary: "Answer"
98133
99-
And then make this:
134+
assign_species_name <- function(df, species){
135+
return_df <- left_join(df, species, by = c("species" = "alpha_code"))
136+
return(return_df)
137+
}
100138
101139
```
102-
common_name year total_predated pred_count
103-
American Golden-Plover 2003 4 62
104-
American Golden-Plover 2004 22 93
105-
American Golden-Plover 2005 0 72
106-
American Golden-Plover 2006 0 193
107-
American Golden-Plover 2007 12 154
108-
American Golden-Plover 2008 22 328
109-
American Golden-Plover 2009 92 443
140+
141+
142+
::: callout-note
143+
## Document your funtion inserting Roxygen skeleton and adding the necesary description.
144+
Place the cursor inside your function, In the top menu go to Code > Insert Roxygen skeleton. Document parameters, return and write one example.
145+
146+
:::
147+
148+
```{r}
149+
#| code-summary: "Answer"
150+
151+
#' Title
152+
#'
153+
#' @param df A data frame containing BBL species codes in column `species`
154+
#' @param species A data frame defining BBL species codes with columns `alpha_code` and `common_name`
155+
#'
156+
#' @return A data frame with original data df, plus the common name of species
157+
#' @export
158+
#'
159+
#' @examples `*provide example*`
160+
161+
162+
assign_species_name <- function(df, species){
163+
return_df <- left_join(df, species, by = c("species" = "alpha_code"))
164+
return(return_df)
165+
}
166+
110167
```
111168

112-
REMOVE ARROW BELOW
113-
-->
169+
170+
::: callout-note
171+
## Use your function to clean names of each data frame
172+
173+
<!--Finalize this question-->
174+
175+
:::
176+
177+
178+
179+
::: callout-note
180+
## Challenge
181+
182+
**Optional Extra Challenge**: For a little extra challenge, try to incorporate an `if` statement that looks for `NA` values in the common name field you are adding. What other conditionals might you include to make your function smarter?
183+
:::
184+
185+
186+
114187

115188
::: {.callout-caution icon="false"}
116189

materials/session_17.qmd

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ title: "Practice Session II"
33
title-block-banner: true
44
execute:
55
eval: false
6+
format:
7+
html:
8+
code-link: true
9+
code-fold: true
10+
code-summary: "Answer"
11+
code-overflow: wrap
612
---
713

814

915

10-
1116
{{< include /sections/r-practice-function-cleaning-data.qmd >}}
1217

0 commit comments

Comments
 (0)