STAT120 - Statistical Computing in R

Homework 6

Part I

This part is based on the running dataset. It is a dataset that records my running mileage by day of the week. You’ll be using the pipe operator and data manipulation functions, so make sure you load relevant packages.

  1. On average, which day of the week do I tend to run the most miles? Calculate the average mileage for Monday, Tuesday, etc. to compare.
Show solution
library(tidyverse)
running <- read.csv('../../../dataset/running.csv')
summary(running)
        Week          Mon              Tue              Wed        
 Length   :317   Min.   : 0.000   Min.   : 0.000   Min.   : 0.000  
 N.unique :317   1st Qu.: 0.000   1st Qu.: 0.000   1st Qu.: 0.000  
 N.blank  :  0   Median : 0.000   Median : 1.000   Median : 2.000  
 Min.nchar:  6   Mean   : 1.508   Mean   : 1.915   Mean   : 2.256  
 Max.nchar:  8   3rd Qu.: 2.000   3rd Qu.: 3.000   3rd Qu.: 4.000  
                 Max.   :20.000   Max.   :14.000   Max.   :17.000  
      Thu              Fri              Sat              Sun        
 Min.   : 0.000   Min.   : 0.000   Min.   : 0.000   Min.   : 0.000  
 1st Qu.: 0.000   1st Qu.: 0.000   1st Qu.: 0.000   1st Qu.: 0.000  
 Median : 1.500   Median : 0.000   Median : 0.000   Median : 2.000  
 Mean   : 2.164   Mean   : 2.071   Mean   : 1.909   Mean   : 4.078  
 3rd Qu.: 4.000   3rd Qu.: 4.000   3rd Qu.: 2.500   3rd Qu.: 7.000  
 Max.   :19.000   Max.   :12.000   Max.   :21.000   Max.   :26.200  
  1. Convert the data to the following long format and store it as a separate data frame (running.long). Note that your output might be sorted by Day (i.e., the first rows correspond to all the Monday data). Print out the first 5 rows of your data.
Show solution
running.long <- running %>% 
  pivot_longer(cols = Mon:Sun, names_to = 'Day', values_to = 'Miles')

running.long %>%
  filter(Week=='8/8/16')
# A tibble: 7 × 3
  Week   Day   Miles
  <chr>  <chr> <dbl>
1 8/8/16 Mon     2.5
2 8/8/16 Tue     6  
3 8/8/16 Wed     2  
4 8/8/16 Thu     3  
5 8/8/16 Fri     0  
6 8/8/16 Sat     2  
7 8/8/16 Sun     7  
  1. With the long format, calculate the average mileage for Monday, Tuesday, ect. and generate the following output.
Show solution
running.long %>%
  group_by(Day) %>%
  summarise(average.miles=mean(Miles)) %>%
  arrange(desc(average.miles))
# A tibble: 7 × 2
  Day   average.miles
  <chr>         <dbl>
1 Sun            4.08
2 Wed            2.26
3 Thu            2.16
4 Fri            2.07
5 Tue            1.91
6 Sat            1.91
7 Mon            1.51
  1. Print out the top 10 days when I ran the most miles (you should get the following information), using both running (wide format) and running.long (long format). Discuss which format is easier in this case.
Show solution
# wide format: difficult
# maybe: manually sort through Monday to Sunday to find top 10
running %>%
  arrange(desc(Mon)) %>%
  slice(1:10)
       Week Mon Tue Wed Thu Fri Sat  Sun
1  12/17/18  20   0   6 0.0 6.0   0  0.0
2  12/23/19  20   0   3 1.5 4.5   0  7.0
3  10/29/18  15   0   3 3.0 5.0  12  0.0
4   9/25/17  12   0   0 5.0 0.0   0  0.0
5   10/2/17  12   0   0 0.0 0.0   0 13.0
6    8/2/21  12   0   4 0.0 4.0   0  8.0
7   6/13/22  12   0   5 0.0 0.0   0  0.0
8   9/19/16  11   0   0 0.0 5.0   0 13.0
9   10/8/18  10   0   4 3.0 0.0   0 13.1
10  1/14/19  10   0   4 1.0 0.0   0 26.2
Show solution
# long format: easy
running.long %>%
  arrange(desc(Miles)) %>%
  slice(1:10)
# A tibble: 10 × 3
   Week     Day   Miles
   <chr>    <chr> <dbl>
 1 11/13/17 Sun    26.2
 2 1/14/19  Sun    26.2
 3 5/6/19   Sun    26.2
 4 1/6/20   Sun    26.2
 5 11/15/21 Sun    26.2
 6 4/8/19   Sat    21  
 7 10/23/17 Sun    20  
 8 11/26/18 Sun    20  
 9 12/17/18 Mon    20  
10 4/1/19   Sun    20  
  1. Reproduce the following two graphs with running.long (which is much easier than using the wide format!). They are essentially the same graph, but the first one is done with the base R boxplot function and the second one is done with ggplot2 (with theme_classic).
Show solution
boxplot(running.long$Miles ~ running.long$Day,
        col='salmon',
        pch=16,
        xlab='Day',
        ylab='Miles')

Show solution
running.long %>% 
  ggplot(aes(x=Day,y=Miles)) +
  geom_boxplot(fill='salmon') +
  theme_classic()

Part II

Take a look at the dataset USArrests in base R. Run the following code to extract the state names and store it as a separate variable in the data frame.

USArrests$state <- row.names(USArrests)

The following graph is generated using the maps package and based on the variable UrbanPop in the USArrests dataset. Reproduce this graph (you can visualize other variables in the USArrests dataset instead of UrbanPop, if you prefer). You can find sample code from Lecture 6 here.

Show solution
library(maps)

USArrests$state <- row.names(USArrests) %>% tolower()
mymap <- map_data(map="state")

# Combine crime data to map data
mymap <- left_join(mymap, USArrests, by=c('region'='state'))

# Visualize
mymap %>%
  ggplot(aes(x=long,y=lat,group=group, fill=UrbanPop)) +
  geom_polygon(color='darkblue') +
  coord_fixed(1.4) +
  theme_void() +
  labs(fill='Urban population percentage') +
  scale_fill_gradient(low="yellow", high="blue")