You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
49
51
50
-
-`Utqiagvik_adult_shorebird_banding.csv`
51
-
-`Utqiagvik_chick_shorebird_banding.csv`
52
+
-`Utqiagvik_predator_surveys.csv`
53
+
-`Utqiagvik_nest_data.csv`
52
54
53
55
**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.
54
56
@@ -66,51 +68,122 @@ This is a handy package that requires a moderate amount of knowledge of `html` t
66
68
67
69
## Write a function that will translate species codes into common names.
68
70
69
-
<!--
70
-
EDIT FROM HERE
71
71
72
72
::: callout-note
73
73
### 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()`)
74
75
75
-
fsfsfsf
76
76
:::
77
77
78
+
```{r}
79
+
#| code-summary: "Answer"
80
+
81
+
## When reading from a file in your data folder in your Rpoj
## 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
+
```
78
100
79
101
80
102
::: 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.
82
106
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
+
85
109
:::
86
110
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
+
87
121
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
+
:::
89
129
90
-
Make this:
91
130
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"
98
133
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
+
}
100
138
101
139
```
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
+
110
167
```
111
168
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?
0 commit comments