# Set file pathsweather_temp_path <-here("data", "monthly_nyc_weather.csv")weather_rain_path <-here("data", "monthly_nyc_rain.csv")# Function to preprocess weather datapreprocess_weather_data <-function() {# Load temperature and rainfall data temp_df <-read_csv(weather_temp_path) rain_df <-read_csv(weather_rain_path)# Convert Month format to date temp_df <- temp_df %>%mutate(Date =as.Date(paste0(Month, "01"), format ="%Y%m%d")) rain_df <- rain_df %>%mutate(Date =as.Date(paste0(Month, "01"), format ="%Y%m%d"))# Rename columns for clarity temp_df <- temp_df %>%rename(Temperature = Value, TempAnomaly = Anomaly) rain_df <- rain_df %>%rename(Rainfall = Value, RainAnomaly = Anomaly)# Merge temperature and rainfall data weather_df <- temp_df %>%inner_join(rain_df, by =c("Date"))# Extract date components for analysis weather_df <- weather_df %>%mutate(Year =year(Date),Month =month(Date),# Create season featureSeason =case_when( Month %in%c(12, 1, 2) ~"Winter", Month %in%c(3, 4, 5) ~"Spring", Month %in%c(6, 7, 8) ~"Summer",TRUE~"Fall" ) )return(weather_df)}# Process weather dataweather_df <-preprocess_weather_data()
Rows: 147 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (3): Month, Value, Anomaly
ℹ 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.
Rows: 147 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (3): Month, Value, Anomaly
ℹ 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.
traffic_path <-here("data", "Automated_Traffic_Volume_Counts_20250505.csv")# Function to preprocess traffic datapreprocess_traffic_data <-function(sample_size =NULL) {# Load traffic data with sampling if needed due to sizeif (!is.null(sample_size)) { traffic_df <-read_csv(traffic_path, n_max = sample_size) } else { traffic_df <-read_csv(traffic_path) }# Handle missing values traffic_df <- traffic_df %>%filter(!is.na(Vol))# Create datetime column and extract components traffic_df <- traffic_df %>%mutate(DateTime =as.POSIXct(paste(Yr, M, D, HH, MM, sep ="-"), format ="%Y-%m-%d-%H-%M"),Year =year(DateTime),Month =month(DateTime),Day =day(DateTime),DayOfWeek =wday(DateTime) -1, # 0 = Sunday, 6 = SaturdayHour =hour(DateTime),# Create features for time of dayTimeOfDay =case_when( Hour >=6& Hour <10~"Morning", Hour >=10& Hour <16~"Midday", Hour >=16& Hour <20~"Evening",TRUE~"Night" ),# Create weekday/weekend featureIsWeekend =if_else(DayOfWeek >=5, 1, 0) )return(traffic_df)}# Process traffic data (with sampling due to large file size)traffic_df <-preprocess_traffic_data(sample_size =100000)# Show summarysummary(traffic_df)
2.1.3 Emergency Response Data Preprocessing
Code
emergency_path <-here("data", "911_Open_Data_Local_Law_119_20250505.csv")# Function to preprocess emergency response datapreprocess_emergency_data <-function() { emergency_df <-read_csv(emergency_path)# Extract date from 'Month Name' column emergency_df <- emergency_df %>%mutate(Date =as.Date(paste0("01 ", `Month Name`), format ="%d %Y / %m"),Year =year(Date),Month =month(Date) )return(emergency_df)}# Process emergency dataemergency_df <-preprocess_emergency_data()
Rows: 10166 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): Month Name, Agency, Description, Borough
dbl (2): # of Incidents, Response Times
ℹ 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.