library(tidyr)

#install.packages("tidyverse")
library(tidyverse)
acs <- rio::import("https://github.com/marctrussler/IDS-Data/raw/refs/heads/main/ACSCountyData.csv")

acs.south <- acs[acs$census.region=="south",]

acs.south$high.uninsured <- acs.south$percent.no.insurance>10

states <- unique(acs.south$state.abbr)
avg.high.uninsured <- rep(NA, length(states))

for(i in 1:length(states)){
  avg.high.uninsured[i] <- mean(acs.south$high.uninsured[acs.south$state.abbr==states[i]])
}

cbind(states, avg.high.uninsured)

acs |> 
  filter(census.region=="south") |> 
  mutate(high.uninsured=percent.no.insurance>10) |> 
  group_by(state.abbr) |> 
  summarise(avg.high.uninsured=mean(high.uninsured))

acs$high.insurance <- NA
acs$high.insurance[acs$percent.no.insurance<20] <- "Yes"


#install.packages("nycflights13")
library(nycflights13)
#Load this data into our environment
data(flights)

glimpse(flights)

flights[flights$dep_delay>120,]

filter(flights, dep_delay>120)

filter(flights, month==1 & day==1)

filter(flights, month==1 | month==2)

jan1 <- filter(flights, month==1 &  day ==1)

distinct(flights)

distinct(flights, dest)

distinct(flights, origin, dest)

distinct(flights, origin, dest, .keep_all = T)

flights[order(flights$dep_delay),]

arrange(flights, dep_delay)

arrange(flights, desc(dep_delay))
#out of curiosity that's a delay of: 
1301/60
#21 hours!

arrange(flights, month, day, dep_time)

summarize(flights, 
          mean(air_time,na.rm=T), 
          sd(air_time, na.rm=T),
          median(air_time,na.rm=T)
)

summarize(flights, 
          mean = mean(air_time,na.rm=T), 
          sd = sd(air_time, na.rm=T),
          median = median(air_time,na.rm=T)
)

flights$speed_old <- flights$distance/flights$air_time*60
head(flights$speed_old)

mutate(flights, 
       speed = distance/air_time*60)

mutate(flights, 
       speed = distance/air_time*60,
       .before=year)


#plot(density(flights$speed))

#Unclear if we will have done ggplot at this point.
#ggplot(flights, mapping = aes(x = speed)) + 
#  geom_density()

flights <- mutate(flights, 
       speed = distance/air_time*60)

plot(density(flights$speed,na.rm=T))
ggplot(flights, mapping = aes(x = speed)) + 
  geom_density()

select(flights, year, month, day)

select(flights, year:day)

select(flights, where(is.character))

select(flights, -year)

select(flights, starts_with("dep"))

select(flights, ends_with("time"))

select(flights, contains("dep"))

flight <- rename(flights, 
       tail.num = tailnum,
       speed.other = speed_old)
names(flights)

relocate(flights, time_hour)

relocate(flights, year:dep_time, .after = time_hour)

relocate(flights, starts_with("arr"), .before = dep_time)

library(nycflights13)
#Create new dataset
flights.base <- flights

#Create speed variable
flights.base$speed <- flights.base$distance/flights.base$air_time*60

#Filter to IAH
flights.houston <- flights.base[flights.base$dest=="IAH",]

#Select variables that we care about
flights.houston <- flights.houston[c("dest", "speed", "year","month", "day", "flight", "origin")]

#Re-order
flights.houston <- flights.houston[order(flights.houston$speed, decreasing = T),]

#View
head(flights.houston)

library(tidyverse)
#Create new dataset
flights.tidy <- flights
#Filter to IAH
flights.tidy <- filter(flights.tidy, dest=="IAH")
#Create speed variable
flights.tidy <- mutate(flights.tidy, speed = distance/air_time*60)
#Select on the variables we are interested in
flights.tidy <- select(flights.tidy,dest,speed, year, month,day, flight, origin)
#Put the highest speed first
flights.tidy <- arrange(flights.tidy, desc(speed))
#View the datset
flights.tidy


filter(flights,dest=="IAH") |> 
mutate(speed = distance/air_time*60) |> 
select(dest,speed, year, month,day, flight, origin) |> 
arrange(desc(speed))

#select(flights, speed, time) |> 
#  mutate(speed = distance/air_time*60)

flights |> 
  filter(dest=="IAH") |> 
  mutate(speed = distance/air_time*60) |> 
  select(dest,speed, year, month,day, flight, origin) |> 
  arrange(desc(speed))

iah.speed <- flights |> 
  filter(dest=="IAH") |> 
  mutate(speed = distance/air_time*60) |> 
  select(dest,speed, year, month,day, flight, origin) |> 
  arrange(desc(speed))

flights |> 
  filter(dest=="IAH") |> 
  mutate(speed = distance/air_time*60) |> 
  select(dest,speed, year, month,day, flight, origin) |> 
  arrange(desc(speed)) -> iah.speed

flights |> 
  filter(origin == "LGA",
         month==5, 
         day==1, 
         dep_delay>100) |>
  select(tailnum, dest)

flights |> 
  group_by(month)

flights |> 
  group_by(month) |> 
  summarize(avg.delay = mean(dep_delay,na.rm=T))

flights |> 
  group_by(month) |> 
  summarize(avg.delay = mean(dep_delay,na.rm=T),
            obs = n(), 
            range.delay = diff(range(dep_delay, na.rm=T)))

flights |> 
  group_by(year, month,day)

daily <- flights |> 
  group_by(year, month,day) %>%
  summarize(n = n(),
            avg.delay = mean(dep_delay, na.rm=T))
daily

daily |> 
  summarize(avg.daily.delay = mean(avg.delay))

daily |> 
  ungroup() |> 
  summarize(avg.daily.delay = mean(avg.delay))

