dplyr
basicsdplyrDuring ANLY 512 we will be studying the theory and practice of
data visualization. We will be using R and the
packages within R to assemble data and construct many
different types of visualizations. Before we begin studying data
visualizations we need to develop some data wrangling skills. We will
use these skills to wrangle our data into a form that we can use for
visualizations.
The objective of this assignment is to introduce you to R Studio,
Rmarkdown, the tidyverse and more specifically the dplyr
package.
Each question is worth 5 points.
To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Canvas. Please make sure that this link is hyper linked and that I can see the visualization and the code required to create it.
Question #1
Use the nycflights13 package and the flights data frame to answer the following questions: a.What month had the highest proportion of cancelled flights? b.What month had the lowest?
library(nycflights13)
unique(flights$year)
## [1] 2013
cancelledFlights <- flights %>%
group_by(month) %>%
summarize(
cancelled = sum(is.na(dep_time)),
totalFLights = n(),
cancelledProportion = cancelled / n()
) %>%
arrange(cancelledProportion)
cancelledFlights
# 6/2013 i.e. June 2013 has highest proportion of cancelled flights
# 10/2013 i.e october 2013 has least proportion of cancelled flights
Question #2
Consider the following pipeline:
library(tidyverse)
# mtcars %>%
# group_by(cyl) %>%
# summarize(avg_mpg = mean(mpg)) %>%
# filter(am == 1)
mtcars %>%
filter(am == 1) %>%
group_by(cyl) %>%
summarize(avgMpg = mean(mpg))
#What is the problem with this pipeline?
#we can't filter by am since it is not included after the group by so we filter before grouping
Question #3
Define two new variables in the Teams data frame in the
pkg Lahman() package.
batting average (BA). Batting average is the ratio of hits (H) to at-bats (AB)
slugging percentage (SLG). Slugging percentage is total bases divided by at-bats (AB). To compute total bases, you get 1 for a single, 2 for a double, 3 for a triple, and 4 for a home run.
library(Lahman)
Teams <- Teams %>%
mutate(BA = H / AB) %>%
mutate(SLG = (H + 2 * X2B + 3 * X3B + 4 * HR) / AB)
head(Teams$BA)
## [1] 0.3104956 0.2700669 0.2765599 0.2386059 0.2870370 0.3200625
head(Teams$SLG)
## [1] 0.5021866 0.4431438 0.4603710 0.3324397 0.3960114 0.5144418
Question #4
Using the Teams data frame in the
pkg Lahman() package. display the top-5 teams ranked in
terms of slugging percentage (SLG) in Major League Baseball history.
Repeat this using teams since 1969. Slugging percentage is total bases
divided by at-bats.To compute total bases, you get 1 for a single, 2 for
a double, 3 for a triple, and 4 for a home run.
library(Lahman)
Teams %>%
select(yearID, teamID, SLG) %>%
arrange(desc(SLG)) %>%
head(5)
Teams %>%
select(yearID, teamID, SLG) %>%
filter(yearID >= 1969) %>%
arrange(desc(SLG)) %>%
head(5)
Question #5
Use the Batting, Pitching, and
People tables in the pkg Lahman() package to
answer the following questions.
a.Name every player in baseball history who has accumulated at least 300 home runs (HR) and at least 300 stolen bases (SB). You can find the first and last name of the player in the Master data frame. Join this to your result along with the total home runs and total bases stolen for each of these elite players.
Similarly, name every pitcher in baseball history who has accumulated at least 300 wins (W) and at least 3,000 strikeouts (SO).
Identify the name and year of every player who has hit at least 50 home runs in a single season. Which player had the lowest batting average in that season?
library(Lahman)
#a
Batting %>%
group_by(playerID) %>%
summarize(homeRuns = sum(HR), stolen_bases = sum (SB)) %>%
filter(homeRuns >= 300 & stolen_bases >= 300) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(nameFirst, nameLast, nameGiven, homeRuns, stolen_bases)
#b
Pitching %>%
group_by(playerID) %>%
summarize(wins = sum(W), strikeouts = sum(SO)) %>%
filter(wins >= 300 & strikeouts >= 3000) %>%
inner_join(People, by = c('playerID' = 'playerID')) %>%
select(nameFirst, nameLast, nameGiven, wins, strikeouts)
#c
Batting %>%
group_by(playerID, yearID) %>%
summarize(homeRuns = sum(HR), battingAvg = sum(H)/sum(AB)) %>%
filter(homeRuns >= 50) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(yearID, nameFirst, nameLast, nameGiven, homeRuns, battingAvg) %>%
arrange(battingAvg)
#Pete Alonso A.K.A Peter Morgan has the lowest batting average in the season