I will be looking at the following three data sets for this project:
#Tidying I first load the data from Github
mta_df <- read_csv("https://raw.githubusercontent.com/Raharijao/Spring2024_Data_607_CUNYSPS/main/Week6/MTA_Daily_Ridership_Data__Beginning_2020_20240303.csv")
## Rows: 1461 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Date
## dbl (14): Subways: Total Estimated Ridership, Subways: % of Comparable Pre-P...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
I delete some of the columns, as I will not need some of them for the analysis. I am left with the date and the total estimated ridership of the subway, buses, LIRR and Metro-North.
#Get all the column names
print(colnames(mta_df))
## [1] "Date"
## [2] "Subways: Total Estimated Ridership"
## [3] "Subways: % of Comparable Pre-Pandemic Day"
## [4] "Buses: Total Estimated Ridership"
## [5] "Buses: % of Comparable Pre-Pandemic Day"
## [6] "LIRR: Total Estimated Ridership"
## [7] "LIRR: % of Comparable Pre-Pandemic Day"
## [8] "Metro-North: Total Estimated Ridership"
## [9] "Metro-North: % of Comparable Pre-Pandemic Day"
## [10] "Access-A-Ride: Total Scheduled Trips"
## [11] "Access-A-Ride: % of Comparable Pre-Pandemic Day"
## [12] "Bridges and Tunnels: Total Traffic"
## [13] "Bridges and Tunnels: % of Comparable Pre-Pandemic Day"
## [14] "Staten Island Railway: Total Estimated Ridership"
## [15] "Staten Island Railway: % of Comparable Pre-Pandemic Day"
mta_df_v1 <- mta_df[,c("Date",
"Subways: Total Estimated Ridership",
"Buses: Total Estimated Ridership",
"LIRR: Total Estimated Ridership",
"Metro-North: Total Estimated Ridership"
)]
old_names <- c("Subways: Total Estimated Ridership",
"Buses: Total Estimated Ridership",
"LIRR: Total Estimated Ridership",
"Metro-North: Total Estimated Ridership")
new_names <- c("Subway", "Bus", "LIRR", "Metro_North")
names(mta_df_v1)[names(mta_df_v1) %in% old_names] <- new_names
I turn the data from a wide format to a long format. I turned all of the columns exept for the Date
mta_df_v1 <- pivot_longer(mta_df_v1,
cols = -c(Date),
names_to = "type",
values_to = "ridership")
Check for NA and delete with data conversion (for date, and for ridership)
print(sum(is.na(mta_df_v1$ridership)))
## [1] 1
mta_df_v1 <- mta_df_v1 %>%
filter(!is.na(ridership))
mta_df_v1$Date <- as.Date(mta_df_v1$Date, format = "%m/%d/%Y")
mta_df_v1$year <- year(mta_df_v1$Date)
#Divide by 1000 to reduce the scale a bit
mta_df_v1$ridership <- (mta_df_v1$ridership)/1000
#Analysis
Summary of 2020,2021,2022 average ridership for the transportation types
This graph displays the average ridership (by thousand people, this was done because the initial number’s scale was too big) of the different type of transportation by year. Overall, the subway and the bus enjoys the most ridership, LIRR and Metro North are significantly lower. However, while the average for the subway and buses have decreased between 2023 and 2024 (this might be due to the year just starting) the average ridership of LIRR and Metro North seems to be increasing as the year passes.
mta_df_v2 <- mta_df_v1 %>%
group_by(year,type) %>%
summarize(mean_m = mean(ridership))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
ggplot(data = mta_df_v2, aes(x = year, y = mean_m, group = type, color = type)) +
geom_line()
Subway vs Buses in 2020
In 2020, the subway definitely got more ridership than the bus at all time. The subway got as much as 5000 thousands riders prior to April, and buses had as much as 2000 thousands. However, around the end of March, both experienced a huge drop in ridership. Buses dropped as low as a few hundred riders.The subway maintained some riders after the drop, but the buses did not recover until August/September.
mta_df_v3 <- mta_df_v1 %>%
filter(type == "Subway" | type == "Bus", year == 2020)
ggplot(data = mta_df_v3, aes(x = Date, y = ridership, group = type, color = type)) +
geom_line()
Highest ridership in 2020
mta_df_v4 <- mta_df_v1 %>%
filter(year == 2020) %>%
group_by(type) %>%
summarize(total_ridership = sum(ridership)) %>%
arrange(desc(total_ridership))
mta_df_v4
## # A tibble: 4 × 2
## type total_ridership
## <chr> <dbl>
## 1 Subway 370097.
## 2 Bus 147388.
## 3 LIRR 17730.
## 4 Metro_North 11489.
##Language diversity
#Tidying Read the file
lang_df <- read_csv("https://raw.githubusercontent.com/Raharijao/Spring2024_Data_607_CUNYSPS/main/Week6/language_diversity.csv")
## Rows: 444 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Continent, Country, Measurement
## dbl (1): Value
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Turn the data into wide format:
lang_df_1 <- lang_df %>%
pivot_wider(names_from = Measurement, values_from = Value)
#Analysis
Are some continent more diverse than others in term of languages?
lang_df_2 <- lang_df_1 %>%
group_by(Continent) %>%
summarize(sum_l = sum(Langs))
#ggplot(data = lang_df_2, aes(x = Continent, y = sum_l)) +
#geom_bar()
Does a high population mean a higher amount of language diversity Does larger area mean higher language diversity?
##Population data for 4 states
#Tidying
pop_df <- read_csv("https://raw.githubusercontent.com/nk014914/Data-607/main/Population_data.csv")
## Rows: 5 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Name, Abbreviations
## dbl (7): FIPS, 2015 Population, 2016 Population, 2017 Population, 2018 Popul...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Renamed Columns:
old_names <- c("2015 Population",
"2016 Population",
"2017 Population",
"2018 Population",
"2019 Population",
"2020 Population")
new_names <- c("2015", "2016", "2017", "2018","2019","2020")
names(pop_df)[names(pop_df) %in% old_names] <- new_names
Filtered out blank rows, and turned the table into long format
pop_df <- pop_df %>%
filter(!is.na(Name))
pop_df_1 <- pivot_longer(pop_df,
cols = -c(Name,FIPS,Abbreviations),
names_to = "year",
values_to = "count")