# Install/load libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.2 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── 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
library(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
library(here)
## here() starts at C:/Users/SHAMBHAVI SINHA/OneDrive/semester1_mcrp/urban_analytics_R/urban_analytics
library(tmap)
library(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
library(kableExtra)
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
# Load POI data from previous assignment
google_places_POI <- readRDS("poi_data_sf.rds")
#removing the duplicates
dupl_google_POI<- google_places_POI %>%
distinct(.keep_all = TRUE)
nrow(google_places_POI)
## [1] 1153
nrow(dupl_google_POI)
## [1] 676
str_extract(dupl_google_POI$places.formattedAddress, "\\d{5}")
## [1] "35203" "35205" "35205" "35205" "35205" "35205" "35205" "35205" "35205"
## [10] "35205" "35205" "35205" "35205" "35205" "35205" "35205" "35205" "35205"
## [19] "35205" "35205" "35205" "35206" "35212" "35206" "35206" "35222" "35222"
## [28] "35222" "35212" "35210" "35213" "35212" "35212" "35211" "35211" "35208"
## [37] "35212" "35212" "35212" "35212" "35204" "35204" "35211" "35204" "35205"
## [46] "35203" "35211" "35211" "35212" "35212" "35233" "35294" "35233" "35233"
## [55] "35233" "35233" "35294" "35233" "35233" "35294" "35233" "35233" "35233"
## [64] "35233" "35233" "35233" "35233" "35233" "35233" "35294" "35211" "35211"
## [73] "35211" "35211" "35211" "35211" "35211" "35211" "35211" "35211" "35211"
## [82] "35211" "35211" "35211" "35211" "35211" "35211" "35211" "35233" "35118"
## [91] "35127" "35221" "35221" "35221" "35211" "35211" "35206" "35206" "35206"
## [100] "35233" "35233" "35233" "35205" "35205" "35202" "35205" "35207" "35207"
## [109] "35217" "35217" "35217" "35217" "35203" "35203" "35203" "35203" "35203"
## [118] "35203" "35203" "35203" "35203" "35203" "35203" "35203" "35233" "35202"
## [127] "35222" "35222" "35222" "35212" "35212" "35235" "35235" "35235" "35235"
## [136] "35212" "35212" "35217" "35217" "35212" "35215" "35203" "35204" "35204"
## [145] "35204" "35204" "35224" "35224" "35064" "35218" "35218" "35218" "35064"
## [154] "35218" "35218" "35064" "35205" "35205" "35205" "35205" "35209" "35209"
## [163] "35205" "35205" "35206" "35206" "35206" "35206" "35061" "35234" "35218"
## [172] "35218" "35204" "35204" "35217" "35217" "35211" "35211" "35211" "35208"
## [181] "35211" "35211" "35211" "35211" "35205" "35205" "35211" "35211" "35211"
## [190] "35211" "35211" "35211" "35211" "35211" "35211" "35211" "35211" "35211"
## [199] "35211" "35221" "35221" "35215" "35215" "35215" "35215" "35215" "35215"
## [208] "35215" "35215" "35205" "35205" "35205" "35205" "35205" "35205" "35205"
## [217] "35205" "35205" "35209" "35209" "35211" "35209" "35211" "35211" "35234"
## [226] "35218" "35214" "35214" "35214" "35214" "35205" "35233" "35205" "35205"
## [235] "35294" "35205" "35233" "35233" "35294" "35233" "35205" "35213" "35210"
## [244] "35213" "35210" "35213" "35210" "35233" "35233" "35294" "35233" "35233"
## [253] "35294" "35233" "35294" "35222" "35233" "35294" "35233" "35233" "35233"
## [262] "35233" "35233" "35294" "35233" "35233" "35233" "35205" "35205" "35205"
## [271] "35215" "35215" "35235" "35235" "35211" "35228" "35228" "35228" "35228"
## [280] "35221" "35020" "35222" "35222" "35218" "35243" "35243" "35243" "35243"
## [289] "35209" "35205" "35205" "35205" "35205" "35209" "35209" "35203" "35203"
## [298] "35210" "35210" "35210" "35206" "35206" "35218" "35206" "35206" "35206"
## [307] "35206" "35233" "35205" "35205" "35205" "35205" "35205" "35205" "35233"
## [316] "35233" "35233" "35205" "35205" "35233" "35294" "35233" "35233" "35294"
## [325] "35294" "35222" "35222" "35222" "35234" "35205" "35210" "35203" "35203"
## [334] "35203" "35215" "35217" "35213" "35213" "35222" "35208" "35208" "35211"
## [343] "35209" "35209" "35206" "35206" "35218" "35218" "35218" "35218" "35218"
## [352] "35205" "35205" "35203" "35209" "35207" "35207" "35207" "35064" "35211"
## [361] "35203" "35203" "35203" "35203" "35203" "35228" "35130" "35215" "35215"
## [370] "35206" "35206" "35214" "35214" "35214" "35214" "35214" "35214" "35210"
## [379] "35210" "35094" "35210" "35210" "35210" "35210" "35235" "35235" "35235"
## [388] "35235" "35235" "35235" "35235" "35235" "35235" "35235" "35235" "35215"
## [397] "35208" "35208" "35211" "35226" "35020" "35020" "35228" "35210" "35207"
## [406] "35235" "35235" "35233" "35205" "35205" "35205" "35222" "35213" "35211"
## [415] "35212" "35208" "35211" "35226" "35226" "35226" "35226" "35209" "35211"
## [424] "35242" "35242" "17051" "17035" "35210" "35210" "35210" "35210" "35210"
## [433] "35213" "35210" "35242" "35242" "35242" "35242" "35068" "35208" "35208"
## [442] "35214" "35214" "35214" "35222" "35233" "35243" "35243" "35243" "35243"
## [451] "35243" "35243" "35243" "35243" "35243" "35243" "35243" "35243" "35243"
## [460] "35243" "35243" "35243" "35209" "35209" "35209" "35209" "35209" "35209"
## [469] "35209" "35210" "35243" "35243" "35243" "35243" "35243" "35243" "35243"
## [478] "35243" "35243" "35243" "35243" "35243" "35243" "35243" "35243" "35243"
## [487] "35243" "35243" "35209" "35209" "35215" "35215" "35206" "35210" "35210"
## [496] "35210" "35210" "35210" "35235" "35235" "35235" "35209" "35209" "35209"
## [505] "35209" "35209" "35209" "35209" "35209" "35209" "35209" "35209" "35209"
## [514] "35212" "35212" "35210" "35213" "35210" "35213" "35210" "35211" "35211"
## [523] "35211" "35211" "35209" "35209" "35224" "35224" "35064" "35064" "35207"
## [532] "35204" "35204" "35204" "35207" "35208" "35208" "35211" "35211" "35211"
## [541] "35215" "35215" "35215" "35215" "35215" "35215" "35215" "35235" "35235"
## [550] "35235" "35235" "35235" "35235" "35235" "35205" "35205" "35203" "35205"
## [559] "35294" "35294" "35205" "35233" "35205" "35218" "35206" "35206" "35206"
## [568] "35215" "35215" "35215" "35215" "35206" "35206" "35206" "35223" "35242"
## [577] "35223" "35242" "35118" "35127" "35127" "35127" "35127" "35224" "35023"
## [586] "35023" "35023" "35061" "35023" "35023" "35023" "35023" "35023" "35224"
## [595] "35023" "35023" "35023" "35211" "35211" "35211" "35173" "35173" "35233"
## [604] "35233" "35233" "35233" "35233" "35233" "35294" "35205" "35294" "35242"
## [613] "35242" "17051" "17035" "35094" "35242" "35242" "35242" "35242" "35210"
## [622] "35210" "35242" "35242" "35242" "35242" "35242" "35242" "35223" "35210"
## [631] "35205" "35205" "35209" "35205" "35205" "35294" "35242" "35213" "35210"
## [640] "35213" "35224" "35218" "35207" "35215" "35215" "35215" "35215" "35222"
## [649] "35223" "35209" "35209" "35223" "35209" "35223" "35209" "35209" "35211"
## [658] "35211" "35211" "35211" "35211" "35211" "35211" "35206" "35206" "35206"
## [667] "35215" "35206" "35206" "35242" "35243" "35243" "35243" "35173" "35226"
## [676] "35210"
#Check if the delimiters are working as expected
str_split_fixed(dupl_google_POI$places.formattedAddress, pattern = "AL |, USA", n = 3) %>% head(10)
## [,1] [,2] [,3]
## [1,] "2316 7th Ave N V, Birmingham, " "35203" ""
## [2,] "2700 10th Ave S #305, Birmingham, " "35205" ""
## [3,] "2700 10th Ave S, Birmingham, " "35205" ""
## [4,] "2700 10th Ave S #505, Birmingham, " "35205" ""
## [5,] "2728 10th Ave S, Birmingham, " "35205" ""
## [6,] "2728 10th Ave S, Birmingham, " "35205" ""
## [7,] "Suite 505 Bld 3, 833 St. Vincent's Drive, Birmingham, " "35205" ""
## [8,] "833 St Vincents Dr Building 3, Suite 403, Birmingham, " "35205" ""
## [9,] "2728 10th Ave S Suite 300, Birmingham, " "35205" ""
## [10,] "810 St Vincents Dr, Birmingham, " "35205" ""
str_split_fixed(dupl_google_POI$places.formattedAddress, pattern = "AL |, USA", n = 3) %>% .[,2]
## [1] "35203"
## [2] "35205"
## [3] "35205"
## [4] "35205"
## [5] "35205"
## [6] "35205"
## [7] "35205"
## [8] "35205"
## [9] "35205"
## [10] "35205"
## [11] "35205"
## [12] "35205"
## [13] "35205"
## [14] "35205"
## [15] "35205"
## [16] "35205"
## [17] "35205"
## [18] "35205"
## [19] "35205"
## [20] "35205"
## [21] "35205"
## [22] "35206"
## [23] "35212"
## [24] "35206"
## [25] "35206"
## [26] "35222"
## [27] "35222"
## [28] "35222"
## [29] "35212"
## [30] "35210"
## [31] "35213"
## [32] "35212"
## [33] "35212"
## [34] "35211"
## [35] "35211"
## [36] "35208"
## [37] "35212"
## [38] "35212"
## [39] "35212"
## [40] "35212"
## [41] "35204"
## [42] "35204"
## [43] "35211"
## [44] "35204"
## [45] "35205"
## [46] "35203"
## [47] "35211"
## [48] "35211"
## [49] "35212"
## [50] "35212"
## [51] "35233"
## [52] "35294"
## [53] "35233"
## [54] "35233"
## [55] "35233"
## [56] "35233"
## [57] "35294"
## [58] "35233"
## [59] "35233"
## [60] "35294"
## [61] "35233"
## [62] "35233"
## [63] "35233"
## [64] "35233"
## [65] "35233"
## [66] "35233"
## [67] "35233"
## [68] "35233"
## [69] "35233"
## [70] "35294"
## [71] "35211"
## [72] "35211"
## [73] "35211"
## [74] "35211"
## [75] "35211"
## [76] "35211"
## [77] "35211"
## [78] "35211"
## [79] "35211"
## [80] "35211"
## [81] "35211"
## [82] "35211"
## [83] "35211"
## [84] "35211"
## [85] "35211"
## [86] "35211"
## [87] "35211"
## [88] "35211"
## [89] "35233"
## [90] "35118"
## [91] "35127"
## [92] "35221"
## [93] "35221"
## [94] "35221"
## [95] "35211"
## [96] "35211"
## [97] "35206"
## [98] "35206"
## [99] "35206"
## [100] "35233"
## [101] "35233"
## [102] "35233"
## [103] "35205"
## [104] "35205"
## [105] "35202"
## [106] "35205"
## [107] "35207"
## [108] "35207"
## [109] "35217"
## [110] "35217"
## [111] "35217"
## [112] "35217"
## [113] "35203"
## [114] "35203"
## [115] "35203"
## [116] "35203"
## [117] "35203"
## [118] "35203"
## [119] "35203"
## [120] "35203"
## [121] "35203"
## [122] "35203"
## [123] "35203"
## [124] "35203"
## [125] "35233"
## [126] "35202"
## [127] "35222"
## [128] "35222"
## [129] "35222"
## [130] "35212"
## [131] "35212"
## [132] "35235"
## [133] "35235"
## [134] "35235"
## [135] "35235"
## [136] "35212"
## [137] "35212"
## [138] "35217"
## [139] "35217"
## [140] "35212"
## [141] "35215"
## [142] "35203"
## [143] "35204"
## [144] "35204"
## [145] "35204"
## [146] "35204"
## [147] "35224"
## [148] "35224"
## [149] "35064"
## [150] "35218"
## [151] "35218"
## [152] "35218"
## [153] "35064"
## [154] "35218"
## [155] "35218"
## [156] "35064"
## [157] "35205"
## [158] "35205"
## [159] "35205"
## [160] "35205"
## [161] "35209"
## [162] "35209"
## [163] "35205"
## [164] "35205"
## [165] "35206"
## [166] "35206"
## [167] "35206"
## [168] "35206"
## [169] "35061"
## [170] "35234"
## [171] "35218"
## [172] "35218"
## [173] "35204"
## [174] "35204"
## [175] "35217"
## [176] "35217"
## [177] "35211"
## [178] "35211"
## [179] "35211"
## [180] "35208"
## [181] "35211"
## [182] "35211"
## [183] "35211"
## [184] "35211"
## [185] "35205"
## [186] "35205"
## [187] "35211"
## [188] "35211"
## [189] "35211"
## [190] "35211"
## [191] "35211"
## [192] "35211"
## [193] "35211"
## [194] "35211"
## [195] "35211"
## [196] "35211"
## [197] "35211"
## [198] "35211"
## [199] "35211"
## [200] "35221"
## [201] "35221"
## [202] "35215"
## [203] "35215 and Two Other Locations: ArtPlay/UAB and, 136 Roebuck Dr, Birmingham, "
## [204] "35215"
## [205] "35215"
## [206] "35215"
## [207] "35215"
## [208] "35215"
## [209] "35215"
## [210] "35205"
## [211] "35205"
## [212] "35205"
## [213] "35205"
## [214] "35205"
## [215] "35205"
## [216] "35205"
## [217] "35205"
## [218] "35205"
## [219] "35209"
## [220] "35209"
## [221] "35211"
## [222] "35209"
## [223] "35211"
## [224] "35211"
## [225] "35234"
## [226] "35218"
## [227] "35214"
## [228] "35214"
## [229] "35214"
## [230] "35214"
## [231] "35205"
## [232] "35233"
## [233] "35205"
## [234] "35205"
## [235] "35294"
## [236] "35205"
## [237] "35233"
## [238] "35233"
## [239] "35294"
## [240] "35233"
## [241] "35205"
## [242] "35213"
## [243] "35210"
## [244] "35213"
## [245] "35210"
## [246] "35213"
## [247] "35210"
## [248] "35233"
## [249] "35233"
## [250] "35294"
## [251] "35233"
## [252] "35233"
## [253] "35294"
## [254] "35233"
## [255] "35294"
## [256] "35222"
## [257] "35233"
## [258] "35294"
## [259] "35233"
## [260] "35233"
## [261] "35233"
## [262] "35233"
## [263] "35233"
## [264] "35294"
## [265] "35233"
## [266] "35233"
## [267] "35233"
## [268] "35205"
## [269] "35205"
## [270] "35205"
## [271] "35215"
## [272] "35215"
## [273] "35235"
## [274] "35235"
## [275] "35211"
## [276] "35228"
## [277] "35228"
## [278] "35228"
## [279] "35228"
## [280] "35221"
## [281] "35020"
## [282] "35222"
## [283] "35222"
## [284] "35218"
## [285] "35243"
## [286] "35243"
## [287] "35243"
## [288] "35243"
## [289] "35209"
## [290] "35205"
## [291] "35205"
## [292] "35205"
## [293] "35205"
## [294] "35209"
## [295] "35209"
## [296] "35203"
## [297] "35203"
## [298] "35210"
## [299] "35210"
## [300] "35210"
## [301] "35206"
## [302] "35206"
## [303] "35218"
## [304] "35206"
## [305] "35206"
## [306] "35206"
## [307] "35206"
## [308] "35233"
## [309] "35205"
## [310] "35205"
## [311] "35205"
## [312] "35205"
## [313] "35205"
## [314] "35205"
## [315] "35233"
## [316] "35233"
## [317] "35233"
## [318] "35205"
## [319] "35205"
## [320] "35233"
## [321] "35294"
## [322] "35233, Birmingham, "
## [323] "35233"
## [324] "35294"
## [325] "35294"
## [326] "35222"
## [327] "35222"
## [328] "35222"
## [329] "35234"
## [330] "35205"
## [331] "35210"
## [332] "35203"
## [333] "35203"
## [334] "35203"
## [335] "35215"
## [336] "35217"
## [337] "35213"
## [338] "35213"
## [339] "35222"
## [340] "35208"
## [341] "35208"
## [342] "35211"
## [343] "35209"
## [344] "35209"
## [345] "35206"
## [346] "35206"
## [347] "35218"
## [348] "35218"
## [349] "35218"
## [350] "35218"
## [351] "35218"
## [352] "35205"
## [353] "35205"
## [354] "35203"
## [355] "35209"
## [356] "35207"
## [357] "35207"
## [358] "35207"
## [359] "35064"
## [360] "35211"
## [361] "35203"
## [362] "35203"
## [363] "35203"
## [364] "35203"
## [365] "35203"
## [366] "35228"
## [367] "35130"
## [368] "35215"
## [369] "35215"
## [370] "35206"
## [371] "35206"
## [372] "35214"
## [373] "35214"
## [374] "35214"
## [375] "35214"
## [376] "35214"
## [377] "35214"
## [378] "35210"
## [379] "35210"
## [380] "35094"
## [381] "35210"
## [382] "35210"
## [383] "35210"
## [384] "35210"
## [385] "35235"
## [386] "35235"
## [387] "35235"
## [388] "35235"
## [389] "35235"
## [390] "35235"
## [391] "35235"
## [392] "35235"
## [393] "35235"
## [394] "35235"
## [395] "35235"
## [396] "35215"
## [397] "35208"
## [398] "35208"
## [399] "35211"
## [400] "35226"
## [401] "35020"
## [402] "35020"
## [403] "35228"
## [404] "35210"
## [405] "35207"
## [406] "35235"
## [407] "35235"
## [408] "35233"
## [409] "35205"
## [410] "35205"
## [411] "35205"
## [412] "35222"
## [413] "35213"
## [414] "35211"
## [415] "35212"
## [416] "35208"
## [417] "35211"
## [418] "35226"
## [419] "35226"
## [420] "35226"
## [421] "35226"
## [422] "35209"
## [423] "35211"
## [424] "35242"
## [425] "35242"
## [426] "35242"
## [427] "35242"
## [428] "35210"
## [429] "35210"
## [430] "35210"
## [431] "35210"
## [432] "35210"
## [433] "35213"
## [434] "35210"
## [435] "35242"
## [436] "35242"
## [437] "35242"
## [438] "35242"
## [439] "35068"
## [440] "35208"
## [441] "35208"
## [442] "35214"
## [443] "35214"
## [444] "35214"
## [445] "35222"
## [446] "35233"
## [447] "35243"
## [448] "35243"
## [449] "35243"
## [450] "35243"
## [451] "35243"
## [452] "35243"
## [453] "35243"
## [454] "35243"
## [455] "35243"
## [456] "35243"
## [457] "35243"
## [458] "35243"
## [459] "35243"
## [460] "35243"
## [461] "35243"
## [462] "35243"
## [463] "35209"
## [464] "35209"
## [465] "35209"
## [466] "35209"
## [467] "35209"
## [468] "35209"
## [469] "35209"
## [470] "35210"
## [471] "35243"
## [472] "35243"
## [473] "35243"
## [474] "35243"
## [475] "35243"
## [476] "35243"
## [477] "35243"
## [478] "35243"
## [479] "35243"
## [480] "35243"
## [481] "35243"
## [482] "35243"
## [483] "35243"
## [484] "35243"
## [485] "35243"
## [486] "35243"
## [487] "35243"
## [488] "35243"
## [489] "35209"
## [490] "35209"
## [491] "35215"
## [492] "35215"
## [493] "35206"
## [494] "35210"
## [495] "35210"
## [496] "35210"
## [497] "35210"
## [498] "35210"
## [499] "35235"
## [500] "35235"
## [501] "35235"
## [502] "35209"
## [503] "35209"
## [504] "35209"
## [505] "35209"
## [506] "35209"
## [507] "35209"
## [508] "35209"
## [509] "35209"
## [510] "35209"
## [511] "35209"
## [512] "35209"
## [513] "35209"
## [514] "35212"
## [515] "35212"
## [516] "35210"
## [517] "35213"
## [518] "35210"
## [519] "35213"
## [520] "35210"
## [521] "35211"
## [522] "35211"
## [523] "35211"
## [524] "35211"
## [525] "35209"
## [526] "35209"
## [527] "35224"
## [528] "35224"
## [529] "35064"
## [530] "35064"
## [531] "35207"
## [532] "35204"
## [533] "35204"
## [534] "35204"
## [535] "35207"
## [536] "35208"
## [537] "35208"
## [538] "35211"
## [539] "35211"
## [540] "35211"
## [541] "35215"
## [542] "35215"
## [543] "35215"
## [544] "35215"
## [545] "35215"
## [546] "35215"
## [547] "35215"
## [548] "35235"
## [549] "35235"
## [550] "35235"
## [551] "35235"
## [552] "35235"
## [553] "35235"
## [554] "35235"
## [555] "35205"
## [556] "35205"
## [557] "35203"
## [558] "35205"
## [559] "35294"
## [560] "35294"
## [561] "35205"
## [562] "35233"
## [563] "35205"
## [564] "35218"
## [565] "35206"
## [566] "35206"
## [567] "35206"
## [568] "35215"
## [569] "35215"
## [570] "35215"
## [571] "35215 and Two Other Locations: ArtPlay/UAB and, 136 Roebuck Dr, Birmingham, "
## [572] "35206"
## [573] "35206"
## [574] "35206"
## [575] "35223"
## [576] "35242"
## [577] "35223"
## [578] "35242"
## [579] "35118"
## [580] "35127"
## [581] "35127"
## [582] "35127"
## [583] "35127"
## [584] "35224"
## [585] "35023"
## [586] "35023"
## [587] "35023"
## [588] "35061"
## [589] "35023"
## [590] "35023"
## [591] "35023"
## [592] "35023"
## [593] "35023"
## [594] "35224"
## [595] "35023"
## [596] "35023"
## [597] "35023"
## [598] "35211"
## [599] "35211"
## [600] "35211"
## [601] "35173"
## [602] "35173"
## [603] "35233"
## [604] "35233"
## [605] "35233"
## [606] "35233"
## [607] "35233"
## [608] "35233"
## [609] "35294"
## [610] "35205"
## [611] "35294"
## [612] "35242"
## [613] "35242"
## [614] "35242"
## [615] "35242"
## [616] "35094"
## [617] "35242"
## [618] "35242"
## [619] "35242"
## [620] "35242"
## [621] "35210"
## [622] "35210"
## [623] "35242"
## [624] "35242"
## [625] "35242"
## [626] "35242"
## [627] "35242"
## [628] "35242"
## [629] "35223"
## [630] "35210"
## [631] "35205"
## [632] "35205"
## [633] "35209"
## [634] "35205"
## [635] "35205"
## [636] "35294"
## [637] "35242"
## [638] "35213"
## [639] "35210"
## [640] "35213"
## [641] "35224"
## [642] "35218"
## [643] "35207"
## [644] "35215"
## [645] "35215"
## [646] "35215"
## [647] "35215"
## [648] "35222"
## [649] "35223"
## [650] "35209"
## [651] "35209"
## [652] "35223"
## [653] "35209"
## [654] "35223"
## [655] "35209"
## [656] "35209"
## [657] "35211"
## [658] "35211"
## [659] "35211"
## [660] "35211"
## [661] "35211"
## [662] "35211"
## [663] "35211"
## [664] "35206"
## [665] "35206"
## [666] "35206"
## [667] "35215"
## [668] "35206"
## [669] "35206"
## [670] "35242"
## [671] "35243"
## [672] "35243"
## [673] "35243"
## [674] "35173"
## [675] "35226"
## [676] "35210"
# Identify list-columns in the dataset
for (col in colnames(dupl_google_POI)) {
if (is.list(dupl_google_POI[[col]])) {
print(col)
}
}
## [1] "places.types"
## [1] "geometry"
dupl_google_POI$places.types[[1]]
## [1] "secondary_school" "primary_school" "preschool"
## [4] "child_care_agency" "school" "point_of_interest"
## [7] "establishment"
dupl_google_POI$places.types[[2]]
## [1] "hospital" "health" "point_of_interest"
## [4] "establishment"
poi_flat <- dupl_google_POI %>%
mutate(places.types = places.types %>%
map_chr(.,1))
poi_flat <- dupl_google_POI %>%
mutate(places.types = places.types %>%
map_chr(.,2))
head(poi_flat$places.types)
## [1] "primary_school" "health" "health"
## [4] "doctor" "child_care_agency" "health"
# I do not avw reviews column to flatten and make into new table
# Count the number of NAs in each column
poi_flat%>% map_dbl(., ~sum(is.na(.x)))
## places.id places.types
## 0 0
## places.formattedAddress places.displayName.text
## 0 0
## places.displayName.languageCode places.rating
## 0 381
## places.userRatingCount places.priceLevel
## 381 666
## category geometry
## 0 0
#I feel the columns places.rating and places.pricelevel are important for the analysis. Since the POIs I selected is hospital and schools, they did not have much oter columns available as compared to the case of restaurents.
#for the question on removing the missing data,if we need to assess the qality of services in these public infrastructure, a good way would be to look at the affordability and the user ratings. Since, a lot of these places have no rating it seems fair to drop them in the cleaning process.That means a lot of people are not really using and reporting them
#removing the rows with NA in these columns for price and rating
# Drop rows with NA in the critical columns
poi_dropna <- poi_flat %>%
drop_na(places.rating)
# Report row counts
print(paste0("Before: ", nrow(poi_flat)))
## [1] "Before: 676"
print
## function (x, ...)
## UseMethod("print")
## <bytecode: 0x000002a24fd8b498>
## <environment: namespace:base>
# City boundary: Birmingham, Alabama
bham <- tigris::places("AL", progress_bar = FALSE) %>%
filter(NAME == "Birmingham") %>%
st_transform(4326)
## Retrieving data for the year 2024
# Convert cleaned POIs into sf object
poi_sf <- poi_dropna %>%
st_as_sf(coords = c("places.location.longitude", "places.location.latitude"),
crs = 4326)
# Keep only POIs inside Birmingham boundary
poi_sf_in <- poi_sf[st_within(poi_sf, bham, sparse = FALSE), ]
# Report counts
print(paste0("Before: ", nrow(poi_sf)))
## [1] "Before: 295"
print(paste0("After: ", nrow(poi_sf_in)))
## [1] "After: 234"
# There is an extreme drop in the numbers. It turns out half were duplicates while a lot of the rows were dropped as NA values. In an actual analysis I would rather not drop those with NA in rating if the drop i such a huge number.
``` r
# Visualize
tmap_mode("view")
## ℹ tmap mode set to "view".
tm_shape(bham) +
tm_borders() +
tm_shape(poi_sf_in) +
tm_dots(shape = 21,
col = "category", # color by POI type
palette = c("School" = "blue", "Hospital" = "red"),
size = "places.userRatingCount",
popup.vars = c("Name" = "places.displayName.text",
"Category" = "category",
"Rating" = "places.rating",
"Rating Count" = "places.userRatingCount")) +
tm_layout(legend.outside = TRUE)
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_tm_dots()`: migrate the argument(s) related to the scale of the
## visual variable `fill` namely 'palette' (rename to 'values') to fill.scale =
## tm_scale(<HERE>).
## [v3->v4] `tm_dots()`: use 'fill' for the fill color of polygons/symbols
## (instead of 'col'), and 'col' for the outlines (instead of 'border.col').
## Multiple palettes called "blue" found: "kovesi.blue", "tableau.blue". The first one, "kovesi.blue", is returned.
# Print first 10 rows
print(head(poi_sf_in, 10))
## Simple feature collection with 10 features and 9 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -86.78853 ymin: 33.50766 xmax: -86.73155 ymax: 33.53955
## Geodetic CRS: WGS 84
## places.id places.types
## 1 ChIJRZRQ8skbiYgRS9kgxW_eKQI health
## 2 ChIJEY7k2rgbiYgR4U4ZfkiWb_8 doctor
## 3 ChIJIbKpJLYbiYgRxdde-PteaMc doctor
## 4 ChIJ_woYmMkbiYgRBU3Jg80h1CU health
## 5 ChIJV1WV5MkbiYgRKDCSO8XdQdI health
## 6 ChIJu1SQIdkbiYgRZ2UgcDh8vfk health
## 7 ChIJX4MJyAEbiYgRslUeOlR8_N8 health
## 8 ChIJ9-_nZ14aiYgRWqGvfr_sygc tour_agency
## 9 ChIJZd58wboRiYgRiJkbOLkYcTY point_of_interest
## 10 ChIJ142-ho8aiYgRB8PJQERVjZM child_care_agency
## places.formattedAddress
## 1 2700 10th Ave S #305, Birmingham, AL 35205, USA
## 2 2700 10th Ave S #505, Birmingham, AL 35205, USA
## 3 833 St Vincents Dr Building 3, Suite 403, Birmingham, AL 35205, USA
## 4 2728 10th Ave S Suite 300, Birmingham, AL 35205, USA
## 5 810 St Vincents Dr, Birmingham, AL 35205, USA
## 6 Birmingham, AL 35205, USA
## 7 810 St Vincents Dr, Birmingham, AL 35205, USA
## 8 4515 5th Ave S, Birmingham, AL 35222, USA
## 9 6701 Crestwood Blvd, Birmingham, AL 35210, USA
## 10 5512 1st Ave S, Birmingham, AL 35212, USA
## places.displayName.text
## 1 Birmingham Heart Clinic at St. Vincent's Birmingham
## 2 Jason Moellinger, MD
## 3 OrthoSports Associates
## 4 St Vincent's PET Center LLC
## 5 UAB St. Vincent’s Birmingham
## 6 Andrews POB 3 Parking Deck
## 7 Noland Hospital Birmingham
## 8 Southern Skin Divers Supply
## 9 Festival Head Start Center
## 10 James Rushton Early Learning And Family Success Center
## places.displayName.languageCode places.rating places.userRatingCount
## 1 en 4.9 15
## 2 en 4.9 305
## 3 en 3.4 41
## 4 en 3.8 5
## 5 en 4.4 2309
## 6 en 4.2 14
## 7 en 3.7 3
## 8 en 5.0 26
## 9 en 5.0 6
## 10 en 4.0 4
## places.priceLevel category geometry
## 1 <NA> Hospital POINT (-86.78738 33.50772)
## 2 <NA> Hospital POINT (-86.78741 33.50771)
## 3 <NA> Hospital POINT (-86.78766 33.50855)
## 4 <NA> Hospital POINT (-86.78674 33.50831)
## 5 <NA> Hospital POINT (-86.78842 33.50766)
## 6 <NA> Hospital POINT (-86.78708 33.50869)
## 7 <NA> Hospital POINT (-86.78853 33.50829)
## 8 <NA> School POINT (-86.76366 33.5263)
## 9 <NA> School POINT (-86.73155 33.5309)
## 10 <NA> School POINT (-86.75193 33.53955)
# Comment/Observation
# Hospitals show much more variation in ratings compared to schools.
# Some hospitals have extremely high number of user ratings, while schools have fewer ratings and more uniform scores.
# In terms of spatial distribution:
# - Hospitals are fewer, concentrated mainly in UAB Medical District and around main city roads.
# - Schools are more evenly spread across neighborhoods, usually one or more per neighborhood.
library(dplyr)
library(purrr)
# Function to calculate mode
get_mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
# Summary statistics for each category
rating_summary <- poi_sf_in %>%
st_set_geometry(NULL) %>% # remove geometry for easy summary
group_by(category) %>%
summarise(
mean_rating = mean(places.rating, na.rm = TRUE),
median_rating = median(places.rating, na.rm = TRUE),
mode_rating = get_mode(places.rating),
avg_num_ratings = mean(places.userRatingCount, na.rm = TRUE),
total_POIs = n()
)
print(rating_summary)
## # A tibble: 2 × 6
## category mean_rating median_rating mode_rating avg_num_ratings total_POIs
## <chr> <dbl> <dbl> <dbl> <dbl> <int>
## 1 Hospital 4.19 4.4 5 335. 165
## 2 School 4.16 4.8 5 14.6 69
#Hospital ratings are more widely reported and varied, while school ratings are less frequent but tend to be slightly higher.
#Hospitals may dominate user attention in terms of reviews, but schools maintain strong perceived quality.
library(ggplot2)
# Scatter plot of places.rating vs places.priceLevel for hospitals
poi_sf_in %>%
st_set_geometry(NULL) %>%
filter(category == "Hospital") %>%
ggplot(aes(x = places.priceLevel, y = places.rating)) +
geom_jitter(width = 0.2, height = 0) +
geom_smooth(method = "lm", col = "red") +
labs(title = "Hospital Ratings vs Price Level",
x = "Price Level",
y = "Rating")
## `geom_smooth()` using formula = 'y ~ x'
# Same for Schools if needed
poi_sf_in %>%
st_set_geometry(NULL) %>%
filter(category == "School") %>%
ggplot(aes(x = places.priceLevel, y = places.rating)) +
geom_jitter(width = 0.2, height = 0) +
geom_smooth(method = "lm", col = "blue") +
labs(title = "School Ratings vs Price Level",
x = "Price Level",
y = "Rating")
## `geom_smooth()` using formula = 'y ~ x'
#1.
Observation:
The majority of hospital ratings cluster around the “NA” price level.
Ratings mostly range between 3 to 5, with a few low outliers (1–2).
Very few hospitals are labeled as PRICE_LEVEL_MODERATE, but those few have moderate ratings (around 3–4).
All schools are either “NA” or missing another category; there is no other labeled price level.
Ratings are concentrated at 4–5, with a few low ratings (1–2).
Distribution is wider than hospitals for mid-range ratings (3–4).
Most schools have high ratings despite the absence of price information.
Low-rated schools exist, but they are few.
Price level may not be a strong determinant of school rating in this dataset.
library(sf)
library(dplyr)
library(tmap)
# 1. Transform Birmingham boundary to a projected CRS (meters) for grid creation
bham_m <- st_transform(bham, 3857) # Web Mercator for meters
# 2. Create 1km x 1km grid
grid_1km <- st_make_grid(bham_m, cellsize = 1000, square = TRUE) %>%
st_sf(grid_id = 1:length(.)) # Convert to sf object
# 3. Transform POIs to same CRS
poi_sf_m <- st_transform(poi_sf_in, 3857)
# 4. Count POIs per grid
poi_counts <- poi_sf_m %>%
st_join(grid_1km, join = st_within) %>% # Join to grid
st_drop_geometry() %>%
group_by(grid_id, category) %>%
summarise(count = n(), .groups = "drop")
# 5. Spread to wide format (separate columns for School and Hospital counts)
poi_counts_wide <- poi_counts %>%
tidyr::pivot_wider(names_from = category, values_from = count, values_fill = 0)
# 6. Join counts back to grid
grid_counts <- grid_1km %>%
left_join(poi_counts_wide, by = "grid_id")
# 7. Map Schools
tm_shape(grid_counts) +
tm_polygons(col = "School", palette = "Blues", title = "Number of Schools") +
tm_borders() +
tm_layout(title = "School Clusters in Birmingham (1km x 1km)")
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_tm_polygons()`: migrate the argument(s) related to the scale of
## the visual variable `fill` namely 'palette' (rename to 'values') to fill.scale
## = tm_scale(<HERE>).
## [v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'
## [v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`
## [cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Blues" is named
## "brewer.blues"
## Multiple palettes called "blues" found: "brewer.blues", "matplotlib.blues". The first one, "brewer.blues", is returned.
##
## Registered S3 method overwritten by 'jsonify':
## method from
## print.json jsonlite
# 8. Map Hospitals
tm_shape(grid_counts) +
tm_polygons(col = "Hospital", palette = "Reds", title = "Number of Hospitals") +
tm_borders() +
tm_layout(title = "Hospital Clusters in Birmingham (1km x 1km)")
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_tm_polygons()`: migrate the argument(s) related to the scale of
## the visual variable `fill` namely 'palette' (rename to 'values') to fill.scale
## = tm_scale(<HERE>).[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'[v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`[cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Reds" is named
## "brewer.reds"Multiple palettes called "reds" found: "brewer.reds", "matplotlib.reds". The first one, "brewer.reds", is returned.
library(tidycensus)
# Comment/Observation
# Based on ratings, number of reviews, and importance, the top-rated hospital
# on the southwest-east periphery of Birmingham is selected for visit.
# For schools, a highly-rated school in a residential cluster could be chosen.