From c9d1d31f1473643e0bd5ee37f6a6d94a6487b914 Mon Sep 17 00:00:00 2001 From: molliemhendry Date: Fri, 20 Oct 2023 09:22:06 -0600 Subject: [PATCH] coding exercise --- exercise_MHendry.Rmd | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 exercise_MHendry.Rmd diff --git a/exercise_MHendry.Rmd b/exercise_MHendry.Rmd new file mode 100644 index 0000000..c6a7621 --- /dev/null +++ b/exercise_MHendry.Rmd @@ -0,0 +1,89 @@ +--- +title: "Pull Request Exercise with Kenya Climate Data" +output: html_document +date: "`r Sys.Date()`" +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + +## Assignment Instructions: + +#### Follow VERY carefully and in order! + +1. If you haven't yet, **fork** the Week-1-Intro repo from our class GitHub organization (ENV-Data-Sci-FA23). + +2. Make sure your local repository has all the files/changes your instructors added. To do so go to **your** forked Week-1-Intro repo, in the bar along the top click the 'Sync' dropdown and if the button says 'Update Branch', click it. + +3. **SAVE** this file (in the same folder this one is located) with **YOUR** name instead of 'YOURNAME' in the file name. + +4. Complete the sequence of coding tasks below (to the best of your ability). + +5. Save, Add, Commit, and Push your changes to this file. + +6. Go to **your** forked Week-1-Intro Repo, click the 'Contribute' drop down at the top and click the 'Open Pull Request' button (if this is grayed out that means you have not pushed any changes to GitHub yet). + +7. Provide a title, short description/notes of your changes, and most importantly under 'Reviewers' on the right, type in the username of your **assigned PR reviewer** (which you can find in the 'PR_reviewer_assignments.md' document in this repository. When you are all set, submit the pull request. + +8. **Review your assigned pull request**. You should receive a notification either via GitHub (you can check by clicking the 'notifications' icon directly next to your profile photo in the upper right corner), or you will receive an email link to the email you registered your GitHub account to. + + Follow [these guidelines for reviewing a pull request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/reviewing-proposed-changes-in-a-pull-request), give some meaningful feedback, and when you're done 'Approve' the pull request (do NOT close or merge). + +## The coding exercise: + +Work through the steps below to complete this exercise (to the best of your ability) and then save, add, commit and push this file with your coding edits to your forked repo and follow the instructions above for completing the assignment via creating and reviewing a pull request. + +Load in the tidyverse (only package(s) needed for this exercise: + +```{r} +library(tidyverse) +``` + +Read in the 'kenya_county_climates.csv' in the 'data/' folder and assign it to an environmental variable (Hint: use 'read_csv()'). + +```{r} +kenyacc <- read_csv("C:/Users/molli/OneDrive/Desktop/intro_R/ESS-523A/week1/Week-1-Intro/data/kenya_county_climates.csv") +``` + +Write a chain of operations that does the following: + +1. `select()` just the columns that have "Precipitation" in the name (so we are only interested in the precipitation data), AND the 'County' column. + +2. `filter()` all the rows where 'Precipitation_585_90' is **greater than** the mean of all county precipitation values. + +3. `pivot_longer()` all the precipitation columns (so everything *except* 'County'), so we have one column for all precip values and one column with the value's associated variables. + +```{r} + +kenya_precip <- kenyacc %>% + select(Precipitation_585_30, Precipitation_585_50, Precipitation_585_70, Precipitation_585_90, Precipitation_hist, County) %>% + filter(Precipitation_585_90 > mean(c(Precipitation_585_30, Precipitation_585_50, Precipitation_585_70, Precipitation_585_90)), na.rm = TRUE) %>% + pivot_longer(cols = c(Precipitation_585_30, Precipitation_585_50, Precipitation_585_70, Precipitation_585_90), names_to = "precip", values_to = "precip_value") + +``` + +Question: How many counties have predicted precip val?ues greater than the total average for all counties? + +Using the new dataset created above, create a bar plot of precipitation for each time frame for each county using `facet_wrap()` + +```{r} + +n_distinct(kenya_precip$County) + +ggplot(kenya_precip, aes(x = precip, y = precip_value))+ + geom_col() + + facet_wrap(~County) + +``` + +CHALLENGE: + +This is messy though since we have so many counties. Instead, use `map()` to create an individual bar plot for each County (hint: create a vector of county names from the data, and building off the ggplot code you made above, add in a line to `filter()` out each county (i.e., .x) before creating the plot. AND, no more need to `facet_wrap()` here). + +There are many ways to do this! + +```{r} +?map() + +```