-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,543 additions
and
26 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- | ||
title: "Homework 10 - Data Visualization" | ||
params: | ||
number: 10 | ||
purpose: | | ||
The purposes of this assignment are: | ||
> | ||
> - To practice exploring and data frames in R using the **dplyr** package | ||
> - To practice generating charts using the **ggplot2** package | ||
--- | ||
|
||
```{r child = here::here("fragments", "hw.qmd")} | ||
``` | ||
|
||
### Readings | ||
|
||
The readings from the last week will serve as a helpful reference as you complete this assignment. You can review them here: | ||
|
||
<blockquote class="blockquote"> | ||
|
||
```{r} | ||
#| echo: false | ||
#| message: false | ||
#| results: asis | ||
htmltools::HTML(readings_current) | ||
``` | ||
|
||
</blockquote> | ||
|
||
### **Using AI tools** | ||
|
||
> On this assignment, you are encouraged to use ChatGPT and other AI tools (e.g. Github Copilot). But don't just blindly copy-paste code. The code provided by these tools is not perfect, and you will likely need to modify it to get the correct solution. If you do use an AI tools, you must include the prompt(s) you used (using a comment with `#`) to recieve full credit. If you had to change anything to your prompt to get better results, write that down too in your code with a comment. Learn to use tools like ChatGPT as a learning assistant - a tool to help you accomplish the task - rather than just a solutions manual. One version of using it makes you a better and more efficient coder, the other robs you of that. | ||
### 1) Staying organized [5%] | ||
|
||
Download and use [this template](templates/hw10.zip) for your assignment. Inside the "hw10" folder, open and edit the R script called `hw10.R` and fill out your name, GW netID, and the names of anyone you worked with on this assignment. | ||
|
||
### 2) Choose and load some data [5%] | ||
|
||
For this assignment, you will need to find a dataset of your choosing and create **three** summary visualizations. To keep things manageable, choose one of the following datasets from the following libraries. Note that to load any of these data frames, all you need to do is install and load the package. | ||
|
||
**dplyr**: | ||
|
||
- `storms` | ||
- `starwars` | ||
|
||
**ggplot2**: | ||
|
||
- `diamonds` | ||
- `economics` | ||
- `midwest` | ||
- `mpg` | ||
- `msleep` | ||
- `txhousing` | ||
|
||
**dslabs**: | ||
|
||
- `gapminder` | ||
- `movielens` | ||
- `murders` | ||
- `stars` | ||
|
||
### 3) Inspect your data [10%] | ||
|
||
Once you've chosen a data set, open your `hw10.R` file and begin exploring the data (be sure to load the package that contains the dataset at the top of your file). Write some code in code chunks to preview and summarize the data frame using some of the methods we've used in class. You should be able to quickly get an understanding of what variables are included and their nature. Consider the following questions in your exploration (you don't have to write out answers to these questions - just write code to help you answer them by previewing the data in different ways): | ||
|
||
- What is the total size of the data frame? | ||
- What type of data is each variable (numeric, character, logical, date)? | ||
- Do any variables have missing values? Why might that be? | ||
- What are the "boundaries" of each period of observation: | ||
- For numeric variables, what are the min and max values? | ||
- For character variables, what are the unique values in the variable? | ||
- For date variables, what time period do the observations in these data frames span? | ||
|
||
**Do not brush this step off** - the more thoroughly you inspect your dataset, the easier (and better) you data exploration will be. This will be absolutely critical for making your charts. Make sure you take the time to develop an understanding of the variables in your dataset as it is nearly impossible to imagine what different charts might be worth creating otherwise. | ||
|
||
### 4) Make charts [40%] | ||
|
||
Now that you have a basic understanding of the dataset, make some charts to explore the variables in the data and their potential relationships. You may use base R plotting functions or the **ggplot2** package to make your figures, but you must make at least two different types of figures, including: | ||
|
||
1. A scatterplot of involving at least two variables. | ||
2. A bar chart involving at least one variable. | ||
|
||
You can choose to plot whichever variables you wish, but you must be able to interpret the results of your chart. | ||
|
||
### 5) Interpret your charts [15%] | ||
|
||
Below the code for each of your charts, write a description and interpretation of your chart in a comment. Make sure you address at least the following questions: | ||
|
||
1. Describe what variables you are plotting and why. | ||
2. Describe the primary relationship / trend / information you hope the reader will gain from your visualization. | ||
|
||
### 6) Save your charts [15%] | ||
|
||
At the bottom of your `hw10.R` file, write code to save each of your charts in the `plots` folder. Save them as .png files. | ||
|
||
### 7) Read and reflect [SOLO, 10%] | ||
|
||
Read and reflect on the following readings to preview what we will be covering next: | ||
|
||
> - Sections 26.3 (Data Frame Functions) & 26.4 (Plot Functions) in Hadley Wickham's book R4DS: [https://r4ds.hadley.nz/functions.html#data-frame-functions](https://r4ds.hadley.nz/functions.html#data-frame-functions) | ||
> - This blog post on iteration with the {purrr} package: [https://www.rebeccabarter.com/blog/2019-08-19_purrr](https://www.rebeccabarter.com/blog/2019-08-19_purrr) | ||
Afterwards, in a comment (`#`) in your .R file, write a short reflection on what you've learned and any questions or points of confusion you have about what we've covered thus far. This can just few a few sentences related to this assignment, next week's readings, things going on in the world that remind you something from class, etc. If there's anything that jumped out at you, write it down. | ||
|
||
### Submit | ||
|
||
{{< var hw_submit >}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
--- | ||
title: "Homework 11 - Programming with Data" | ||
params: | ||
number: 11 | ||
purpose: | | ||
The purposes of this assignment are: | ||
> | ||
> - Be able to compose functions using the 'tidy evaluation' syntax to work with data. | ||
> - Be able to parse through lists using `purrr::map()` functions. | ||
--- | ||
|
||
```{r child = here::here("fragments", "hw.qmd")} | ||
``` | ||
|
||
### Readings | ||
|
||
The readings from the last week will serve as a helpful reference as you complete this assignment. You can review them here: | ||
|
||
> - Sections 26.3 (Data Frame Functions) & 26.4 (Plot Functions) in Hadley Wickham's book R4DS: [https://r4ds.hadley.nz/functions.html#data-frame-functions](https://r4ds.hadley.nz/functions.html#data-frame-functions) | ||
> - This blog post on iteration with the {purrr} package: [https://www.rebeccabarter.com/blog/2019-08-19_purrr](https://www.rebeccabarter.com/blog/2019-08-19_purrr) | ||
### **Using AI tools** | ||
|
||
> On this assignment, you are encouraged to use ChatGPT and other AI tools (e.g. Github Copilot). But don't just blindly copy-paste code. The code provided by these tools is not perfect, and you will likely need to modify it to get the correct solution. If you do use an AI tools, you must include the prompt(s) you used (using a comment with `#`) to recieve full credit. If you had to change anything to your prompt to get better results, write that down too in your code with a comment. Learn to use tools like ChatGPT as a learning assistant - a tool to help you accomplish the task - rather than just a solutions manual. One version of using it makes you a better and more efficient coder, the other robs you of that. | ||
### 1) Staying organized [SOLO, 5%] | ||
|
||
Download and use [this template](templates/hw11.zip) for your assignment. Inside the "hw11" folder, open and edit the R script called `hw11.R` and fill out your name, GW netID, and the names of anyone you worked with on this assignment. | ||
|
||
## Data Frame Functions | ||
|
||
> For questions 2 - 5, after writing your function, demonstrate it using a data frame of your choice from the `dslabs` package. For example, for question 2, you could use `var_summary(dslabs::movielens, rating)` (so, obviously, you should use a different example). | ||
### 2) `var_summary(df, var)` [SOLO, 10%] | ||
|
||
Write the function `var_summary(df, var)` that takes a data frame (`df`) and a variable (`var`) as inputs, and returns the minimum, maximum, mean, and median value of that variable. The function should remove any `NA` values in `var` when computing these summary statistics. The object returned should be a single data frame / tibble (not a vector). | ||
|
||
### 3) `group_summary(df, var, group_var)` [SOLO, 10%] | ||
|
||
Write the function `group_summary(df, var, group_var)` that takes a data frame (`df`), a variable (`var`), and a grouping variable (`group_var`) as inputs, and returns a summary table showing the count, mean, and standard deviation of the variable `var` grouped by `group_var`. The function should remove any `NA` values in `var` when computing these summary statistics. The object returned should be a single data frame / tibble (not a vector). | ||
|
||
### 4) `var_hist(df, var, bins)` [SOLO, 10%] | ||
|
||
Write the function `var_hist(df, var, bins)` that takes a data frame (`df`), a variable (`var`), and the number of bins (`bins`) as inputs, and returns a histogram of that variable with a user-specified number of bins as a ggplot object. The default number of bins should be `30`. | ||
|
||
### 5) `scatterplot(df, x, y)` [SOLO, 10%] | ||
|
||
Write the function `scatterplot(df, x, y)` that takes a data frame (`df`) and two variables (`x` and `y`) as inputs, and returns a scatter plot of those two variables as a ggplot object. | ||
|
||
## Iteration across lists with `purrr` | ||
|
||
### Problems using `word_list` | ||
|
||
For these questions, we will work with the `sentences` vector that comes loaded with the `stringr` package (which is loaded when you load the `tidyverse` package): | ||
|
||
```{r} | ||
library(tidyverse) | ||
head(stringr::sentences) | ||
``` | ||
|
||
This vector contains lots of random sentences. When we break those sentences into individual words using `str_split()`, we will get a _list_ back where each item in the list is a vector of words: | ||
|
||
```{r} | ||
word_list <- str_split(stringr::sentences, " ") | ||
word_list[1:3] | ||
``` | ||
|
||
We will use this `word_list` for questions 6 - 8. | ||
|
||
### 6) [COLLABORATIVE, 5%] | ||
|
||
Using `map()`, write R code to obtain a vector of how many words are in each item in `word_list`. | ||
|
||
### 7) [COLLABORATIVE, 5%] | ||
|
||
Using `map()`, write R code to obtain a vector of the total number of characters in each item in `word_list`. | ||
|
||
### 8) [COLLABORATIVE, 5%] | ||
|
||
Using `map()`, write R code to obtain a vector of the number of times the word `"the"` appears in each item in `word_list`. Your result should ignore casing, so both `"the"` and `"The"` should count. | ||
|
||
### Problems using `sw_people` | ||
|
||
As we saw in class, the `sw_people` list contains a list of information about each character in Star Wars. You can load the list from the `repurrrsive` package: | ||
|
||
```{r} | ||
library(repurrrsive) | ||
``` | ||
|
||
We will use the `sw_people` and `sw_films` lists for questions 9 & 10. | ||
|
||
### 9) [COLLABORATIVE, 10%] | ||
|
||
Using `map()` and the `sw_films` list, write R code to obtain a vector of integers that contains the number of characters in each Star Wars film. | ||
|
||
### 10) [COLLABORATIVE, 10%] | ||
|
||
Using `map_df()`, create a data frame where each row represents a character from `sw_people`. The columns should contain the following: | ||
|
||
- `name`: The character's name, as a character. | ||
- `height`: The character's height, as a number | ||
- `is_male`: Whether the character's gender is `"male"` (`TRUE` or `FALSE`) | ||
- `film_count`: The number of films they have appeared in, as an integer. | ||
|
||
### 11) [SOLO, 10%] | ||
|
||
For the last problem, write your own homework question that requires the student (you) to use `map()` in the solution. You can use any lists of data you want for your question (e.g. `sw_people`, `sw_films`, `got_chars`, etc.). Then provide the answer to your question. As with all the other questions, if you use an AI tool to help you create and / or solve your question, include the prompt you used and comment on any changes you had to make to improve your outcome. | ||
|
||
### 12) Read and reflect [SOLO, 10%] | ||
|
||
Read and reflect on the following readings to preview what we will be covering next: | ||
|
||
> - Chapter 25 (Web scraping) in Hadley Wickham's book R4DS: [https://r4ds.hadley.nz/webscraping.html](https://r4ds.hadley.nz/webscraping.html) | ||
> - This post on accessing and collecting data with APIs in R: [https://statisticsglobe.com/api-in-r](https://statisticsglobe.com/api-in-r) | ||
Afterwards, in a comment (`#`) in your .R file, write a short reflection on what you've learned and any questions or points of confusion you have about what we've covered thus far. This can just few a few sentences related to this assignment, next week's readings, things going on in the world that remind you something from class, etc. If there's anything that jumped out at you, write it down. | ||
|
||
### Submit | ||
|
||
{{< var hw_submit >}} |
Oops, something went wrong.