Skip to content

Commit

Permalink
breaking down funtion exercise into meaningful steps
Browse files Browse the repository at this point in the history
  • Loading branch information
camilavargasp committed Feb 27, 2024
1 parent 9f6d0ff commit 4eb080c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 29 deletions.
129 changes: 101 additions & 28 deletions materials/sections/r-practice-function-cleaning-data.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ library(dplyr)
library(janitor)
```

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.
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.

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

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:

- `Utqiagvik_adult_shorebird_banding.csv`
- `Utqiagvik_chick_shorebird_banding.csv`
- `Utqiagvik_predator_surveys.csv`
- `Utqiagvik_nest_data.csv`

**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.

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

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

<!--
EDIT FROM HERE

::: callout-note
### Read and explore data
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()`)

fsfsfsf
:::

```{r}
#| code-summary: "Answer"
## When reading from a file in your data folder in your Rpoj
nest_data <- read_csv("data/Utqiagvik_nest_data.csv")
predator_survey <- read_csv("data/ Utqiagvik_predator_surveys.csv")
## When reading using the url
nest_data <- read_csv("https://arcticdata.io/metacat/d1/mn/v2/object/urn%3Auuid%3A982bd2fc-4edf-4da7-96ef-0d11b853102d")
predator_survey <- read_csv("https://arcticdata.io/metacat/d1/mn/v2/object/urn%3Auuid%3A9ffec04c-7e2d-41dd-9e88-b6c2e8c4375e")
## Exploring the data (these functions can also be used to explore nest_data)
colnames(predator_survey)
glimpse(predator_survey)
unique(predator_survey$species)
summary(predator_survey)
```


::: callout-note
## Write functions to clean species code
## How would you translate species codes into common names for one of the data frmes?

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.

- **Hint**: The fastest way to do this involves adding a column to the `data.frame`. Your function will have two arguments
- **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?
**Hint:** `joins`

:::

```{r}
#| code-summary: "Answer"
predator_comm_names <- left_join(predator_survey,
species,
by = c("species" = "alpha_code"))
```



### Visual schematic of data
::: callout-note
## Write a functions to add species common name to any data frame.
How can you generalize the code from the previous question and make it into a function?

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.

:::

Make this:

year common_name pred_count
2003 Glaucous Gull 54
2003 Parasitic Jaeger 2
2003 Pomarine Jaeger 6
2004 Glaucous Gull 69
2004 Long-tailed Jaeger 13
```{r}
#| code-summary: "Answer"
And then make this:
assign_species_name <- function(df, species){
return_df <- left_join(df, species, by = c("species" = "alpha_code"))
return(return_df)
}
```
common_name year total_predated pred_count
American Golden-Plover 2003 4 62
American Golden-Plover 2004 22 93
American Golden-Plover 2005 0 72
American Golden-Plover 2006 0 193
American Golden-Plover 2007 12 154
American Golden-Plover 2008 22 328
American Golden-Plover 2009 92 443


::: callout-note
## Document your funtion inserting Roxygen skeleton and adding the necesary description.
Place the cursor inside your function, In the top menu go to Code > Insert Roxygen skeleton. Document parameters, return and write one example.

:::

```{r}
#| code-summary: "Answer"
#' Title
#'
#' @param df A data frame containing BBL species codes in column `species`
#' @param species A data frame defining BBL species codes with columns `alpha_code` and `common_name`
#'
#' @return A data frame with original data df, plus the common name of species
#' @export
#'
#' @examples `*provide example*`
assign_species_name <- function(df, species){
return_df <- left_join(df, species, by = c("species" = "alpha_code"))
return(return_df)
}
```

REMOVE ARROW BELOW
-->

::: callout-note
## Use your function to clean names of each data frame

<!--Finalize this question-->

:::



::: callout-note
## Challenge

**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?
:::




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

Expand Down
7 changes: 6 additions & 1 deletion materials/session_17.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ title: "Practice Session II"
title-block-banner: true
execute:
eval: false
format:
html:
code-link: true
code-fold: true
code-summary: "Answer"
code-overflow: wrap
---




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

0 comments on commit 4eb080c

Please sign in to comment.