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?
Answer: I assume that a flight is cancelled if it has a missing value of the time of departure. As we can see, the proportion of missing values vary from 0.0164 in September 2013, to 0.0364 in December 2013. Therefore, the highest proportion of cancelled flights was observed in December 2013, and the lowest proportion - in September 2013.
library(nycflights13)
flights %>%
group_by(year, month) %>%
summarize(count_na = sum(is.na(dep_time))/(sum(is.na(dep_time))+sum(!is.na(dep_time))))
## # A tibble: 12 × 3
## # Groups: year [1]
## year month count_na
## <int> <int> <dbl>
## 1 2013 1 0.0193
## 2 2013 2 0.0505
## 3 2013 3 0.0299
## 4 2013 4 0.0236
## 5 2013 5 0.0196
## 6 2013 6 0.0357
## 7 2013 7 0.0319
## 8 2013 8 0.0166
## 9 2013 9 0.0164
## 10 2013 10 0.00817
## 11 2013 11 0.00854
## 12 2013 12 0.0364
Question #2
Consider the following pipeline:
library(tidyverse)
mtcars %>%
group_by(cyl) %>%
summarize(avg_mpg = mean(mpg)) %>%
filter(am == 1)
What is the problem with this pipeline?
Answer: this code is not running, the error is caused because of the operations group_by and summarize() implement changes in the data frame structure: there are two columns left - avg_mpg and cyl. Therefore, there is no am column to filter. It would be better to write like following:
mtcars %>% filter(am == 1) %>% group_by(cyl) %>% summarize(avg_mpg = mean(mpg))
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)
myTeam<-Teams
myTeam<-mutate(myTeam, BA = H/AB)
myTeam<-mutate(myTeam, SLG=(4*HR+3*X3B+2*X2B+H)/AB)
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)
sortedTeam<-arrange(myTeam,desc(SLG)) %>%
select(yearID, teamID, SLG)
head(sortedTeam,5)
## yearID teamID SLG
## 1 2019 HOU 0.6092998
## 2 2019 MIN 0.6071179
## 3 2003 BOS 0.6033975
## 4 2019 NYA 0.5996776
## 5 2020 ATL 0.5964320
sortedSince1969 <- arrange(myTeam, desc(SLG)) %>%
select(yearID,teamID,SLG) %>%
filter(yearID >= 1969)
head(sortedSince1969, 5)
## yearID teamID SLG
## 1 2019 HOU 0.6092998
## 2 2019 MIN 0.6071179
## 3 2003 BOS 0.6033975
## 4 2019 NYA 0.5996776
## 5 2020 ATL 0.5964320
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)
Batting %>%
group_by(playerID) %>%
summarize(SBtotal = sum(SB), HRtotal = sum(HR)) %>%
filter(SBtotal >= 300 & HRtotal >= 300) %>%
inner_join(People, by = c('playerID' = 'playerID')) %>%
select(nameFirst, nameLast, HRtotal, SBtotal)
## # A tibble: 8 × 4
## nameFirst nameLast HRtotal SBtotal
## <chr> <chr> <int> <int>
## 1 Carlos Beltran 435 312
## 2 Barry Bonds 762 514
## 3 Bobby Bonds 332 461
## 4 Andre Dawson 438 314
## 5 Steve Finley 304 320
## 6 Willie Mays 660 338
## 7 Alex Rodriguez 696 329
## 8 Reggie Sanders 305 304
Pitching %>%
group_by(playerID) %>%
summarize(Wtotal = sum(W), SOtotal = sum(SO)) %>%
filter(Wtotal >= 300 & SOtotal >= 3000) %>%
inner_join(People, by = c('playerID' = 'playerID')) %>%
select(nameFirst, nameLast, Wtotal, SOtotal)
## # A tibble: 10 × 4
## nameFirst nameLast Wtotal SOtotal
## <chr> <chr> <int> <int>
## 1 Steve Carlton 329 4136
## 2 Roger Clemens 354 4672
## 3 Randy Johnson 303 4875
## 4 Walter Johnson 417 3509
## 5 Greg Maddux 355 3371
## 6 Phil Niekro 318 3342
## 7 Gaylord Perry 314 3534
## 8 Nolan Ryan 324 5714
## 9 Tom Seaver 311 3640
## 10 Don Sutton 324 3574
Batting %>%
group_by(yearID, playerID) %>%
summarize(HRtotal = sum(HR), BA = sum(H)/sum(AB)) %>%
filter(HRtotal >= 50) %>%
inner_join(People, by = c('playerID' = 'playerID')) %>%
select(nameFirst, nameLast, yearID, HRtotal, BA) %>%
ungroup() %>%
arrange(BA)
## # A tibble: 46 × 5
## nameFirst nameLast yearID HRtotal BA
## <chr> <chr> <int> <int> <dbl>
## 1 Pete Alonso 2019 53 0.260
## 2 Jose Bautista 2010 54 0.260
## 3 Andruw Jones 2005 51 0.263
## 4 Roger Maris 1961 61 0.269
## 5 Greg Vaughn 1998 50 0.272
## 6 Mark McGwire 1997 58 0.274
## 7 Cecil Fielder 1990 51 0.277
## 8 Mark McGwire 1999 65 0.278
## 9 Giancarlo Stanton 2017 59 0.281
## 10 Aaron Judge 2017 52 0.284
## # … with 36 more rows