library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── 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.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(tmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
## (status 2 uses the sf package in place of rgdal)
yelp_all <- readRDS("assigment1.rds")
yelp_dup <- yelp_all[!duplicated(yelp_all$id),]
# Flatten the data frame within coordinates and location columns
yelp_flat <- yelp_dup %>%
jsonlite::flatten() %>%
as_tibble()
# Concatenate what's inside the list-columns (transaction and
# location.display_address)
yelp_concat <- yelp_flat %>%
mutate(transactions = transactions %>%
map_chr(., function(x) str_c(x, collapse=", ")),
location.display_address = location.display_address %>%
map_chr(., function(x) str_c(x, collapse=", ")))
# Custom function that takes the data frame in "categories" column in Yelp data
# and returns a character vector
concate_list <- function(x){
# x is a data frame with columns "alias" and "title" from Yelp$categories
# returns a character vector containing category concatenated titles
titles <- x[["title"]] %>% str_c(collapse = ", ")
return(titles)
}
yelp_flat2 <- yelp_concat %>%
mutate(categories = categories %>% map_chr(concate_list))
yelp_flat2 %>% print(width = 1000)
## # A tibble: 83 × 24
## id alias
## <chr> <chr>
## 1 cp3vImW5gM5QvXJv2L83-Q cafe-hot-wing-jonesboro
## 2 Z7Xc-sFFczx2BQunHAyMwA yummies-cafe-riverdale
## 3 b5i9wWKvOfrJ9nh9ni63AQ kennys-cafe-forest-park
## 4 wnP0du8lZ7zn4tL9OJJCbA msight-tv-studio-cafe-jonesboro-2
## 5 TkC7CRAUgMeZIBzT_crFyA t-bird-cafe-morrow
## 6 GMKjG2IL0swWR7_MNk9Bjg fresh-healthy-cafe-atlanta
## 7 ry-HJVy23bKXE5AnD0Gbww boars-head-cafe-atlanta
## 8 83G5T9lX1aDmFtDX2EyL3w harvest-and-grounds-atlanta-2
## 9 cgSYX3FwSwyXhDj8ouiFzQ k-wings-café-atlanta
## 10 4oJYkQkRuL0aXdE3R_qRVg cafe-siena-college-park
## name
## <chr>
## 1 Cafe Hot Wing
## 2 Yummies Cafe
## 3 Kenny's Cafe
## 4 Msight TV Studio Cafe
## 5 T-Bird Cafe
## 6 Fresh Healthy Cafe
## 7 Boars Head Cafe
## 8 Harvest & Grounds
## 9 K Wings Café
## 10 Cafe Siena
## image_url
## <chr>
## 1 https://s3-media2.fl.yelpcdn.com/bphoto/Yh_dSjap_Yo2z89x_AMWWg/o.jpg
## 2 https://s3-media3.fl.yelpcdn.com/bphoto/AFRiF7scpJXjo44toFODEQ/o.jpg
## 3 https://s3-media2.fl.yelpcdn.com/bphoto/2Ka_GSSOz9IXl3cfdum3pQ/o.jpg
## 4 https://s3-media3.fl.yelpcdn.com/bphoto/IJD1-GbQCjiZtn1oA3S-4g/o.jpg
## 5 https://s3-media2.fl.yelpcdn.com/bphoto/XfUHP8eWDcgGDBP2Ao2pwQ/o.jpg
## 6 https://s3-media2.fl.yelpcdn.com/bphoto/Z7CV3wr43KPHK1ppdEAP6Q/o.jpg
## 7 https://s3-media3.fl.yelpcdn.com/bphoto/DPFfkdVHt4vP9Irh0rlXxw/o.jpg
## 8 https://s3-media1.fl.yelpcdn.com/bphoto/_bs4qbMk34II2iPmChYmiw/o.jpg
## 9 https://s3-media1.fl.yelpcdn.com/bphoto/SwUd-CdWCkc_7VPO4yyq7Q/o.jpg
## 10 https://s3-media3.fl.yelpcdn.com/bphoto/cjXzh9gfby2_Rqju32mXAQ/o.jpg
## is_closed
## <lgl>
## 1 FALSE
## 2 FALSE
## 3 FALSE
## 4 FALSE
## 5 FALSE
## 6 FALSE
## 7 FALSE
## 8 FALSE
## 9 FALSE
## 10 FALSE
## url
## <chr>
## 1 https://www.yelp.com/biz/cafe-hot-wing-jonesboro?adjust_creative=RVjHz4PIp9h…
## 2 https://www.yelp.com/biz/yummies-cafe-riverdale?adjust_creative=RVjHz4PIp9hJ…
## 3 https://www.yelp.com/biz/kennys-cafe-forest-park?adjust_creative=RVjHz4PIp9h…
## 4 https://www.yelp.com/biz/msight-tv-studio-cafe-jonesboro-2?adjust_creative=R…
## 5 https://www.yelp.com/biz/t-bird-cafe-morrow?adjust_creative=RVjHz4PIp9hJEoXg…
## 6 https://www.yelp.com/biz/fresh-healthy-cafe-atlanta?adjust_creative=RVjHz4PI…
## 7 https://www.yelp.com/biz/boars-head-cafe-atlanta?adjust_creative=RVjHz4PIp9h…
## 8 https://www.yelp.com/biz/harvest-and-grounds-atlanta-2?adjust_creative=RVjHz…
## 9 https://www.yelp.com/biz/k-wings-caf%C3%A9-atlanta?adjust_creative=RVjHz4PIp…
## 10 https://www.yelp.com/biz/cafe-siena-college-park?adjust_creative=RVjHz4PIp9h…
## review_count categories rating
## <int> <chr> <dbl>
## 1 27 Cafes 3
## 2 78 Soul Food, Breakfast & Brunch, Cafes 4.5
## 3 18 Cafes, Breakfast & Brunch 4.5
## 4 1 Food Delivery Services, Cafes 1
## 5 2 Cafes, American (Traditional), Breakfast & Brunch 5
## 6 103 Juice Bars & Smoothies, Cafes 3.5
## 7 112 Cafes, Delis 3
## 8 170 Cafes 2
## 9 30 Cafes 3
## 10 8 Coffee & Tea, Cafes 2.5
## transactions price phone display_phone distance
## <chr> <chr> <chr> <chr> <dbl>
## 1 "delivery, pickup" $ "+17704713200" "(770) 471-3200" 980.
## 2 "pickup, delivery" <NA> "+16785452849" "(678) 545-2849" 1295.
## 3 "pickup, delivery" $ "+14049689185" "(404) 968-9185" 1199.
## 4 "" <NA> "+16785323622" "(678) 532-3622" 3142.
## 5 "" <NA> "" "" 2556.
## 6 "" $ "+14042090907" "(404) 209-0907" 4095.
## 7 "delivery" $$ "+17709533300" "(770) 953-3300" 4296.
## 8 "" $ "+14047621577" "(404) 762-1577" 3617.
## 9 "pickup, delivery" $ "+17709910720" "(770) 991-0720" 3267.
## 10 "" $$ "" "" 4429.
## coordinates.latitude coordinates.longitude location.address1
## <dbl> <dbl> <chr>
## 1 33.6 -84.4 "6595 Tara Blvd"
## 2 33.5 -84.4 "7523 Highway 85"
## 3 33.6 -84.4 "4542 Old Dixie Hwy"
## 4 33.5 -84.4 ""
## 5 33.6 -84.3 "6790 Mt Zion Blvd"
## 6 33.6 -84.4 "6000 N Terminal Pkwy"
## 7 33.6 -84.4 "6000 N Terminal Pkwy"
## 8 33.6 -84.4 "6000 N Terminal Pkwy"
## 9 33.6 -84.4 "5631 W Fayetteville Rd"
## 10 33.6 -84.4 "S Terminal Pkwy"
## location.address2
## <chr>
## 1 ""
## 2 ""
## 3 ""
## 4 <NA>
## 5 ""
## 6 "Terminal C, Gate C 45"
## 7 ""
## 8 "Concourse D"
## 9 "Ste 104"
## 10 ""
## location.address3 location.city
## <chr> <chr>
## 1 "" Jonesboro
## 2 <NA> Riverdale
## 3 <NA> Forest Park
## 4 <NA> Jonesboro
## 5 <NA> Morrow
## 6 "" Atlanta
## 7 "Hartsfield-Jackson International Airport, Concourse A" Atlanta
## 8 "" Atlanta
## 9 "" Atlanta
## 10 "Terminal T" College Park
## location.zip_code location.country location.state
## <chr> <chr> <chr>
## 1 30236 US GA
## 2 30274 US GA
## 3 30297 US GA
## 4 30236 US GA
## 5 30260 US GA
## 6 30320 US GA
## 7 30320 US GA
## 8 30320 US GA
## 9 30349 US GA
## 10 30337 US GA
## location.display_address
## <chr>
## 1 6595 Tara Blvd, Jonesboro, GA 30236
## 2 7523 Highway 85, Riverdale, GA 30274
## 3 4542 Old Dixie Hwy, Forest Park, GA 30297
## 4 Jonesboro, GA 30236
## 5 6790 Mt Zion Blvd, Morrow, GA 30260
## 6 6000 N Terminal Pkwy, Terminal C, Gate C 45, Atlanta, GA 30320
## 7 6000 N Terminal Pkwy, Hartsfield-Jackson International Airport, Concourse A,…
## 8 6000 N Terminal Pkwy, Concourse D, Atlanta, GA 30320
## 9 5631 W Fayetteville Rd, Ste 104, Atlanta, GA 30349
## 10 S Terminal Pkwy, Terminal T, College Park, GA 30337
## # ℹ 73 more rows
# Identify whether there exists any NA values.
yelp_flat2 %>%
map_dbl(., function(x) sum(is.na(x)))
## id alias name
## 0 0 0
## image_url is_closed url
## 0 0 0
## review_count categories rating
## 0 0 0
## transactions price phone
## 0 26 0
## display_phone distance coordinates.latitude
## 0 0 0
## coordinates.longitude location.address1 location.address2
## 0 0 16
## location.address3 location.city location.zip_code
## 26 0 0
## location.country location.state location.display_address
## 0 0 0
# No NA in coordinate columns
# Converting yelp_flat2 into a sf object
yelp_sf <- yelp_flat2 %>%
st_as_sf(coords=c("coordinates.longitude", "coordinates.latitude"), crs = 4326)
# Make sure two layers have same crs
tract <- st_read("tract.shp")
## Reading layer `tract' from data source
## `D:\S2\Intro to Urban Analytics\Assignment2\tract.shp' using driver `ESRI Shapefile'
## Simple feature collection with 50 features and 2 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -84.45893 ymin: 33.35246 xmax: -84.24386 ymax: 33.64888
## Geodetic CRS: WGS 84
tract <- st_transform(tract, crs = st_crs(yelp_sf))
# Delete points outside boundary
yelp_in <- yelp_sf[tract %>% st_union(), ,op = st_intersects]
After the cleaning, there are 60 POIs in Cafe or Chinese food category in Clayton county.
Census tracts that have high average rating mostly located at the northeast area of the county.
# Spatial join
census_yelp <- st_join(tract, yelp_in, join = st_intersects)
yelp_census <- st_join(yelp_in, tract, join = st_intersects)
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(census_yelp %>% group_by(GEOID) %>% summarise(rating=mean(rating))) +
tm_polygons(col = "rating", style = "quantile")
The most frequent rating is 3.0 and most of the rating range between 3-3.5. This is also the range that has the most reviews.
Table <- yelp_in %>%
group_by(rating) %>%
summarise(Frequency = n(), Review_total = sum(review_count))
print(Table)
## Simple feature collection with 9 features and 3 fields
## Geometry type: GEOMETRY
## Dimension: XY
## Bounding box: xmin: -84.44778 ymin: 33.42683 xmax: -84.27193 ymax: 33.64272
## Geodetic CRS: WGS 84
## # A tibble: 9 × 4
## rating Frequency Review_total geometry
## <dbl> <int> <int> <GEOMETRY [°]>
## 1 1 1 1 POINT (-84.3541 33.52121)
## 2 1.5 2 107 MULTIPOINT ((-84.43909 33.63795), (-84.36428 33…
## 3 2 3 224 MULTIPOINT ((-84.40266 33.54511), (-84.42924 33…
## 4 2.5 9 214 MULTIPOINT ((-84.44345 33.64083), (-84.44778 33…
## 5 3 16 410 MULTIPOINT ((-84.43912 33.64157), (-84.44681 33…
## 6 3.5 13 1194 MULTIPOINT ((-84.43883 33.64099), (-84.43939 33…
## 7 4 8 226 MULTIPOINT ((-84.34284 33.573), (-84.41314 33.5…
## 8 4.5 6 159 MULTIPOINT ((-84.40078 33.42683), (-84.41692 33…
## 9 5 2 3 MULTIPOINT ((-84.31303 33.56789), (-84.41248 33…
Most of the POIs are in $ category. From the limited data we have, looks like cheaper POIs have a higher rating on average.
Table2 <- yelp_in %>%
group_by(price) %>%
summarise(Frequency = n(), Rating_ave = mean(rating))
print(Table2)
## Simple feature collection with 4 features and 3 fields
## Geometry type: GEOMETRY
## Dimension: XY
## Bounding box: xmin: -84.44778 ymin: 33.42683 xmax: -84.27193 ymax: 33.64272
## Geodetic CRS: WGS 84
## # A tibble: 4 × 4
## price Frequency Rating_ave geometry
## <chr> <int> <dbl> <GEOMETRY [°]>
## 1 $ 30 3.15 MULTIPOINT ((-84.43909 33.63795), (-84.44472 33.60…
## 2 $$ 11 3.09 MULTIPOINT ((-84.43883 33.64099), (-84.43912 33.64…
## 3 $$$$ 1 2 POINT (-84.40266 33.54511)
## 4 <NA> 18 3.58 MULTIPOINT ((-84.44345 33.64083), (-84.40078 33.42…