Question: Please, solve the question with an RStudio script. The flights data set seen in class is available in the package nycflights 1 3 as a
Please, solve the question with an RStudio script. The flights data set seen in class is available in the package nycflights as a tibble. It contains ontime data for all flights that departed New York City via its three main airports throughout the whole year of The airport from which each flight departed is recorded in the origin column. Load the tidyverse suite of packages, as well as the nycflights package and the flights data therein. a Cancelled flights are defined as those for which no departure or arrival took place. Write code using pipe operators and filter to create a new data set flights which removes any rows with missing values ie NA entries for the depdelay or arrdelay variables Hint: see isna and use the "and" operator & and the negation operator b Define the new variable gain as the difference between the departure delay and the arrival delay, the new variable airhour as the airtime variable reexpressed in hours, and the variable gainperhour as the gain per airhour. Write code using pipe operators and the functions mutate and select which creates a new tibble called flights containing only the gainperhour and origin variables. Hint: you may exploit the fact that mutate allows you to refer to variables you have just created c The ggplot code below plots a kernel density estimate of the gainperhour for each origin airport. However, it overplots all three estimated density curves on the one graph. Modify this code so that it i partitionsfacets the graph into a separate panel for each origin and ii produces histograms with bins rather than density plots. ggplotflights aesx gainperhour, fill origin geomdensity Your final plot should look like this:EWR JFK LGA count origin EWR JFK LGA o o gainperhour NOTE: for parts b and c you may refer to the base R code below, which ultimately produces the same output flights. This code is provided for you to check your results only, do not use this code as a solution to parts b and c flights flightsisnaflights$depdelay & lis.naflights arrdelay flights$gain flights$depdelay flights$arrdelay flights$hours flights$airtime flights$gainperhour flights$gain flightshours flights flightscgainperhour", "origin"
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
