-
Notifications
You must be signed in to change notification settings - Fork 0
/
datawrangling.Rmd
218 lines (161 loc) · 4.89 KB
/
datawrangling.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
---
title: "GGIS 224 Final Project Data Wrangling and Spatial Operations"
author: "Warren Jodjana"
date: "December 14th 2023"
output:
html_document:
theme: cosmo
toc: true
toc_float: true
---
# Environment Setup
```{r, echo=TRUE, message = FALSE}
library(raster)
library(dplyr)
library(sf)
library(terra)
library(tmap)
library(readxl)
library(data.table)
library(spatstat)
```
# Clean & Wrangle Data
## Air Quality Index (AQI)
### Load AQI data using `read_excel`
```{r}
airquality <- read_excel("air-quality-index.xlsx")
glimpse(airquality)
```
### Load tract data using `st_read`
```{r}
airquality_tracts <- st_read("tracts-aqi.geojson")
data.table(airquality_tracts)
```
### Merge data using geoid
```{r}
airquality <- merge(airquality_tracts, airquality, by.x="geoid10", by.y="geoid", all.x=TRUE)
```
### Subset data
```{r}
airquality <- airquality %>%
select(-c("statefp10", "commarea_n", "commarea", "notes", "countyfp10"))
```
### Inspect data using `data.table`
```{r}
data.table(airquality)
```
### Save cleaned data
```{r}
write.csv(airquality, "airquality.csv")
```
## Traffic & Vehicular Emissions
### Load traffic data using `fread`
```{r}
traffic <- fread("traffic-old.csv")
glimpse(traffic)
```
### Convert to spatial format
```{r}
traffic <- st_as_sf(traffic, coords = c("Longitude", "Latitude"), crs = 4326)
```
### Inspect data using `data.table`
```{r}
data.table(traffic)
```
### Save cleaned data
```{r}
write.csv(traffic, "traffic.csv")
```
## Electric Vehicles
### Load Alternative Fuel data using `fread`
```{r}
alternativefuel <- fread("alternativefuel.csv")
```
### Convert to spatial format
```{r}
alternativefuel <- st_as_sf(alternativefuel, coords = c("Longitude", "Latitude"), crs=4326)
data.table(alternativefuel)
```
### Subset data
```{r}
electric_vehicles <- alternativefuel %>%
select(`ID`, `Fuel Type Code`, `Station Name`, `Street Address`, `Location`, `geometry`) %>%
filter(`Fuel Type Code` == "ELEC") %>%
mutate(`Street Address` = ifelse(`Station Name` == "Paul Simon Chicago JCC", "3348 S Kedzie Ave", `Street Address`)) %>%
select(-c("Fuel Type Code"))
```
### Inspect data using `data.table`
```{r}
data.table(electric_vehicles)
```
### Save cleaned data
```{r}
write.csv(electric_vehicles, "electric-vehicles.csv")
```
# Spatial Operations
## Traffic Kernel Density Estimation
```{r}
boundary <- st_read("boundaries.geojson")
```
### Ensure both points and boundaries are the same CRS
```{r}
boundary <- st_transform(boundary, crs = 4326)
```
### Creating a two-dimensional point-pattern object
```{r include=TRUE, warning=FALSE}
coords <- st_coordinates(traffic) # extract coordinates from sf object
bbox <- st_bbox(boundary) # define the observation window (bounding box) using the boundary data
window <- owin(xrange = bbox[c("xmin", "xmax")], yrange = bbox[c("ymin", "ymax")])
traffic.ppp <- ppp(coords[,1], coords[,2], window = window) # Create a ppp (two-dimensional point-pattern) object
```
### Perform Kernel Density Estimation
```{r include=TRUE}
kde <- density(traffic.ppp) # perform kernel density estimation
kde_raster <- raster(kde) # converting KDE back to raster form
kde_raster_masked <- mask(kde_raster, boundary) # clip the KDE in raster form to chicago boundaries
```
### Check bandwith and type of kernel
```{r include=TRUE}
str(kde)
```
<br>
## Kernel Density Estimation on Traffic in Chicago
```{r include=TRUE, warning=FALSE}
tmap_mode("view")
chikde <- tm_shape(boundary) + tm_borders() +
tm_shape(kde_raster_masked) +
tm_raster(title="Traffic Density", style="cont", palette="Blues") +
tm_scale_bar(position = c("left", "bottom"))
chikde
```
## Proximity Analysis
```{r}
nearest_traffic_index <- st_nearest_feature(electric_vehicles, traffic)
nearest_traffic_distances <- st_distance(electric_vehicles, traffic[nearest_traffic_index, ], by_element = TRUE)
tmap_mode("view")
tm <- tm_shape(traffic) +
tm_bubbles(size = "ID", col = "red", border.col = "black", alpha = 0.5) +
tm_shape(electric_vehicles) +
tm_symbols(col = "blue", size = 0.1) +
tm_layout(title = "EV Stations and Nearest Traffic Data Points")
tm
```
## Comparative Analysis
```{r}
tmap_mode("plot")
airquality_summary <- airquality %>%
group_by(geoid10) %>%
summarize(mean_NO2 = mean(CMAQ_NO2, na.rm = TRUE),
mean_PM25 = mean(CMAQ_PM25, na.rm = TRUE),
mean_O3 = mean(CMAQ_O3, na.rm = TRUE))
plot_NO2 <- tm_shape(airquality_summary) +
tm_polygons("mean_NO2", title = "Mean NO2") +
tm_layout(main.title = "Spatial Distribution of NO2", main.title.size = 1)
plot_PM25 <- tm_shape(airquality_summary) +
tm_polygons("mean_PM25", title = "Mean PM2.5") +
tm_layout(main.title = "Spatial Distribution of PM2.5", main.title.size = 1)
plot_O3 <- tm_shape(airquality_summary) +
tm_polygons("mean_O3", title = "Mean O3") +
tm_layout(main.title = "Spatial Distribution of O3", main.title.size = 1)
tmap_arrange(plot_NO2, plot_PM25, plot_O3)
```