This homework has two parts. Part 1 uses lubridate and factors on NYC flight data. Part 2 does a full EDA on the built-in airquality dataset.


Part 1 — NYC Flights: Dates and Factors

#install.packages(c("nycflights13", "lubridate", "zoo", "forcats"))  # if needed
library(nycflights13)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(forcats)
data(flights)

Quick look:

str(flights)
## tibble [336,776 × 19] (S3: tbl_df/tbl/data.frame)
##  $ year          : int [1:336776] 2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 ...
##  $ month         : int [1:336776] 1 1 1 1 1 1 1 1 1 1 ...
##  $ day           : int [1:336776] 1 1 1 1 1 1 1 1 1 1 ...
##  $ dep_time      : int [1:336776] 517 533 542 544 554 554 555 557 557 558 ...
##  $ sched_dep_time: int [1:336776] 515 529 540 545 600 558 600 600 600 600 ...
##  $ dep_delay     : num [1:336776] 2 4 2 -1 -6 -4 -5 -3 -3 -2 ...
##  $ arr_time      : int [1:336776] 830 850 923 1004 812 740 913 709 838 753 ...
##  $ sched_arr_time: int [1:336776] 819 830 850 1022 837 728 854 723 846 745 ...
##  $ arr_delay     : num [1:336776] 11 20 33 -18 -25 12 19 -14 -8 8 ...
##  $ carrier       : chr [1:336776] "UA" "UA" "AA" "B6" ...
##  $ flight        : int [1:336776] 1545 1714 1141 725 461 1696 507 5708 79 301 ...
##  $ tailnum       : chr [1:336776] "N14228" "N24211" "N619AA" "N804JB" ...
##  $ origin        : chr [1:336776] "EWR" "LGA" "JFK" "JFK" ...
##  $ dest          : chr [1:336776] "IAH" "IAH" "MIA" "BQN" ...
##  $ air_time      : num [1:336776] 227 227 160 183 116 150 158 53 140 138 ...
##  $ distance      : num [1:336776] 1400 1416 1089 1576 762 ...
##  $ hour          : num [1:336776] 5 5 5 5 6 5 6 6 6 6 ...
##  $ minute        : num [1:336776] 15 29 40 45 0 58 0 0 0 0 ...
##  $ time_hour     : POSIXct[1:336776], format: "2013-01-01 05:00:00" "2013-01-01 05:00:00" ...
head(flights)
## # A tibble: 6 × 19
##    year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time
##   <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>
## 1  2013     1     1      517            515         2      830            819
## 2  2013     1     1      533            529         4      850            830
## 3  2013     1     1      542            540         2      923            850
## 4  2013     1     1      544            545        -1     1004           1022
## 5  2013     1     1      554            600        -6      812            837
## 6  2013     1     1      554            558        -4      740            728
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## #   tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## #   hour <dbl>, minute <dbl>, time_hour <dttm>
# Q1. Create a column dep_datetime by combining year, month, day, and dep_time into a
#     POSIXct datetime using lubridate's make_datetime().
#     Hint: hour = dep_time %/% 100, minute = dep_time %% 100
#     Show the first 5 rows with year, month, day, dep_time, and dep_datetime.

flights_q1 <- flights %>%
  mutate( 
    dep_datetime = make_datetime(
      year,
      month,
      day,
      dep_time %/% 100,
      dep_time %% 100
    )
  )

flights_q1 %>%
 select(year, month, day, dep_time,dep_datetime) %>%
      slice(1:5)
## # A tibble: 5 × 5
##    year month   day dep_time dep_datetime       
##   <int> <int> <int>    <int> <dttm>             
## 1  2013     1     1      517 2013-01-01 05:17:00
## 2  2013     1     1      533 2013-01-01 05:33:00
## 3  2013     1     1      542 2013-01-01 05:42:00
## 4  2013     1     1      544 2013-01-01 05:44:00
## 5  2013     1     1      554 2013-01-01 05:54:00
# Q2. After creating dep_datetime, use lubridate's month() to filter flights that
#     departed in JUNE 2013. How many flights are there?
#     (Hint: filter(month(dep_datetime) == 6))

flights_q1  %>%
  filter(month(dep_datetime) == 6) %>%
  count()
## # A tibble: 1 × 1
##       n
##   <int>
## 1 27234
# Q3. The carrier column is a character. Convert it to a factor and check its levels.

flights_q3 <- flights %>%
  mutate(carrier_factor = factor(carrier))

levels(flights_q3$carrier_factor)
##  [1] "9E" "AA" "AS" "B6" "DL" "EV" "F9" "FL" "HA" "MQ" "OO" "UA" "US" "VX" "WN"
## [16] "YV"
# Q4. Use fct_collapse() to keep "UA", "AA", and "DL" as their own levels and lump
#     everything else into "Other". Then count flights per recoded carrier level.
#     (Hint: see the fct_collapse demo from the Wrangling Activity Part H)

flights_q4 <- flights_q3 %>%
  mutate(
    carrier_group = fct_collapse(
      carrier_factor,
      UA = "UA",
      AA = "AA",
      DL = "DL",
      Other = c(
        "9E", "AS", "B6", "EV","F9" , 
        "FL", "HA", "MQ", "OO",
        "US", "VX", "WN", "YV"
      )
    )
  )

flights_q4 %>%
  count(carrier_group)
## # A tibble: 4 × 2
##   carrier_group      n
##   <fct>          <int>
## 1 Other         197272
## 2 AA             32729
## 3 DL             48110
## 4 UA             58665
# Q5. Missing data: how many flights have NA for dep_delay? Filter them out and report
#     the remaining row count.
flights %>%
  summarize(missing_dep_delay = sum(is.na(dep_delay)))
## # A tibble: 1 × 1
##   missing_dep_delay
##               <int>
## 1              8255

Part 2 — Airquality EDA

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ ggplot2 4.0.3     ✔ stringr 1.6.0
## ✔ purrr   1.2.2     ✔ tibble  3.3.1
## ✔ readr   2.2.0     ✔ tidyr   1.3.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data("airquality")
# Q6. For Ozone, Temp, and Wind: compute mean, median, sd, min, max
#     (use na.rm = TRUE where needed).
airquality %>% 
  summarize (
    ozone_mean = mean(Ozone, na.rm = TRUE),
    ozone_median = median(Ozone, na.rm = TRUE),
    ozone_sd = sd(Ozone, na.rm = TRUE),
    ozone_min = min(Ozone, na.rm = TRUE),
    ozone_max = max (Ozone, na.rm = TRUE),
    
    Temp_mean = mean(Temp),
    Temp_median = median(Temp),
    Temp_sd = sd(Temp),
    Temp_min = min(Temp),
    Temp_max = max(Temp),

    Wind_mean = mean(Wind),
    Wind_median = median(Wind),
    Wind_sd = sd(Wind),
    Wind_min = min(Wind),
    Wind_max = max(Wind)
    )
##   ozone_mean ozone_median ozone_sd ozone_min ozone_max Temp_mean Temp_median
## 1   42.12931         31.5 32.98788         1       168  77.88235          79
##   Temp_sd Temp_min Temp_max Wind_mean Wind_median  Wind_sd Wind_min Wind_max
## 1 9.46527       56       97  9.957516         9.7 3.523001      1.7     20.7

Q7. Compare the mean and median for each variable. What does the relationship between mean and median suggest about distribution skewness? What does the standard deviation tell you about variability?

For Ozone, The mean is higher than the median, which suggests that the distribution is right_skewed. This means that most ozone values are lower, but a few high ozone values pull the mean upward. For Temp, the mean and median are close to each other, suggesting that the temperature distribution is more symetric. For Wind, the mean and median are also fairly close, so the wind distribution does not appear to be strongly skewed. The standard deviation tells us how much the values vary around the mean. A larger standard deviation means more variability,while a smaller standard deviation means the values are more concentrated near the mean.

# Q8. Make a histogram of Ozone.

ggplot(airquality,aes(x=Ozone)) + 
  geom_histogram(bins=15)
## Warning: Removed 37 rows containing non-finite outside the scale range
## (`stat_bin()`).

Q9. Describe the shape of the Ozone distribution. Any outliers or unusual features? The Ozone Distribution is right skewed. Most observation are concentrated at lower ozone values, while fewer observations appear at higher ozone values. the long right tail suggest that there may be some unusually high ozone observations.

# Q10. Create a new column month_name with full month names (May–September) using case_when.
#      Then make a boxplot of Ozone by month_name.
#      (Hint: case_when was covered in the Wrangling Activity.)
airquality2 <- airquality %>%
  mutate(
    month_name = case_when(  
    Month == 5 ~ "May",
    Month == 6 ~ "June",
     Month == 7 ~ "July",
     Month == 8 ~ "August",
     Month == 9 ~ "September"
  )
)

ggplot(airquality2 , aes (x= month_name, y = Ozone)) +
  geom_boxplot()
## Warning: Removed 37 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

Q11. Which month has the highest median Ozone? Are there outliers in any month?

July appears to have the highest median Ozone level. There are outliers in several months, especially July and September,indicating some unusually high Ozone observations.

# Q12. Scatterplot of Temp vs Ozone, colored by Month.
ggplot(airquality2, aes(x=Temp, y= Ozone,color = month_name)) +
  geom_point()
## Warning: Removed 37 rows containing missing values or values outside the scale range
## (`geom_point()`).

Q13. Is there a visible relationship between temperature and ozone?

Yes, There is a positive relationship between temperature and Ozone.As temperature increases, ozone levels tend to increase as well. The scatterplot shows an upward trend, although there is still some variability in the data.

# Q14. Compute the correlation matrix for Ozone, Temp, and Wind.
#      (Hint: cor(airquality[, c("Ozone","Temp","Wind")], use = "complete.obs"))

cor(airquality[,c("Ozone", "Temp" , "Wind")],
    use = "complete.obs")
##            Ozone       Temp       Wind
## Ozone  1.0000000  0.6983603 -0.6015465
## Temp   0.6983603  1.0000000 -0.5110750
## Wind  -0.6015465 -0.5110750  1.0000000

Q15. Which pair has the strongest correlation? What does that suggest?

The strongest correlation is between Ozone and Temp. This suggests that higher temperatures are generally associated with higher ozone levels.

# Q16. Generate a summary table grouped by Month with: count, mean Ozone, mean Temp,
#      mean Wind for each month.
airquality2 %>%
  group_by(month_name) %>%
  summarize(
    count = n(),
   mean_ozone = mean( Ozone, na.rm = TRUE),
    mean_temp = mean(Temp, na.rm = TRUE),
   mean_wind = mean(Wind, na.rm = TRUE)
  )
## # A tibble: 5 × 5
##   month_name count mean_ozone mean_temp mean_wind
##   <chr>      <int>      <dbl>     <dbl>     <dbl>
## 1 August        31       60.0      84.0      8.79
## 2 July          31       59.1      83.9      8.94
## 3 June          30       29.4      79.1     10.3 
## 4 May           31       23.6      65.5     11.6 
## 5 September     30       31.4      76.9     10.2

Q17. Which month has the highest average ozone? How do temperature and wind vary across months? August has the highest average ozonelevel (59.96),followed closely by July (59.12).These two months also have the highest average temperatures, suggesting that weather is a associated with higher ozone levels. Wind speeds are lower in August and July compared with May and September, which may also contribute to higher ozone concentrations.