Instructions

Completed homework should be submitted on CoursePlus as an Rmd file. Please see the course website for more information about submitting assignments: https://jhudatascience.org/intro_to_r/syllabus.html#submitting-assignments.

Homework will be graded for correct output, not code style. All assignments are due at the end of the course. Please see the course website for more information about grading: https://jhudatascience.org/intro_to_r/syllabus.html#grading.

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

Problem Set

1. Bring the following dataset into R.

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.

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

Justification is just for fun. The main point is that decisions in your analysis should depend on your reasoning not how many lines of code it takes :)

The following questions are not required for full credit, but can make up for any points lost on other questions.

Bonus Practice

A. Bring the following dataset into R.

B. Rename the column Weighted Number With Current Asthma to “asthma_count” using rename(). Replace the original “asthma” object by calling the new dataset “asthma”.

C. Separate Percent (SE) into two separate columns: “percent” and “SE” using the separate() function. Replace the original “asthma” object by calling the new dataset “asthma”.

D. Remove the parentheses around the numbers in the new SE column. You should use a combination of str_replace(), pull() (because stringr package functions work on vectors not dataframes!) and mutate(). Replace the original “asthma” object by calling the new dataset “asthma”.

E. Determine the class of “percent” and “SE”. Can you take the mean values? Why or why not?

F. Use as.numeric() to convert “percent” and “SE” to numeric class. Calculate the mean for both.