Instructions

  1. Please submit your filled out .Rmd file to the assignment drop box on CoursePlus: https://courseplus.jhu.edu/core/index.cfm/go/db:assignment.submit/assignmentID/44235/coID/18000/

  2. All assignments are due by Friday, July 1, 2022 at 11:59pm EST. Please reach out to us if you need help before this time!

## you can add more, or change...these are suggestions
library(tidyverse)
library(readr)
library(dplyr)
library(ggplot2)
library(tidyr)

Problem Set

1. Bring the dataset into R. The dataset is located at: https://jhudatascience.org/intro_to_r/data/mortality.csv. You can use the link, download it, or use whatever method you like for getting the file. Once you get the file, read the dataset in using read_csv() and assign it the name “mort”.

2. Run the colnames() function to take a look at the dataset column names. You should see that there was originally no name for the first column and that R replaced it with “…1”. Rename the first column of “mort” to “country” using the rename() function in dplyr.

3. Select only the numeric type columns (select()). Then, create the variable “year” from column names by using the colnames() function to extract them.

4. What is the typeof() for “year”? If it’s not an integer, turn it into integer form with as.integer().

5. Use the pct_complete() function in the naniar package to determine the percent missing data in “mort”. You might need to load and install naniar!

6. Are there any countries that have a complete record in “mort” across all years? Just look at the output here, don’t reassign it. Hint: look for complete records by dropping all NAs from the dataset using drop_na().

7. Reshape the “complete” data to long form.

8. Bring an additional dataset into R. The dataset is tab-delimited and located at: https://jhudatascience.org/intro_to_r/data/country_pop.txt. You can use the link, download it, or use whatever method you like for getting the file. Once you get the file, read the dataset in using read_tsv() and assign it the name “pop”.

9. Rename the second column in “pop” to “country” and the column “% of world population”, to “percent”. Use the rename() function. Don’t forget to reassign the renamed data to “pop”.

10. Sort the data in “pop” by “Population” from largest to smallest using arrange() and desc(). After sorting, select() “country” to create an one-column tibble of countries ordered by population. Assign this data the name “country_ordered”.

11. Subset “long” based on years 2000-2010, including 2000 and 2010 and call this “long_sub” using & or the between() function. Confirm your filtering worked by looking at the range of “year”. If you’re getting a strange error, make sure you created the “year” column in problem #7.

12. Further subset long_sub. You will filter for specific countries using filter() and the %in% operator. Only include countries in this list: c("Venezuela", "Bahrain", "Estonia", "Iran", "Thailand", "Canada"). Make sure to reassign to “long_sub”.

13. Use pivot_wider() to turn the “year” column of “long_sub” into multiple columns, each representing a different year. Fill values (values_from=) with “mortality”. Assign this pivoted dataset the name “mort_sub”.

14. Using “country_ordered” and “mort_sub”, right_join() the two datasets by “country”. Use the pipe %>% to join this dataset to “pop”, keeping only the data on the lefthand side of the join. Call this “joined”.

15. The values in the table are percentages of the total population (not proportion). Create a new column called “mort_count” that estimates the total number of child deaths per year based on the total population. You can use any year, or an average of all of them, to make your calculation. Whatever you choose, justify your choice. Finally, select() only “country”, “Population”, and “mort_count” and view the data.

Bonus Practice

Please check back soon!