In this report, Clayton, GA is chosen as an example to explore the number and spatial pattern of two kinds of businesses, “restaurants” and “bars”, within this area, with Yelp API.Below are the R codes and visualization.

##01 Choose a County and Download Census Tract Polygons Fulton in GA is chosen. We get access to the census tract data, visualize the tract boundary.

# all packages needed
library(tidycensus)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.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)
library(jsonlite)
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()
## ✖ purrr::flatten() masks jsonlite::flatten()
## ✖ dplyr::lag()     masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(httr)
library(jsonlite)
library(reshape2)
## 
## Attaching package: 'reshape2'
## 
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(here)
## here() starts at /Users/xy/Downloads/GT
library(yelpr)
library(knitr)
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(places)
## Please note that rgdal will be retired during October 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2023/05/15/evolution4.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-7, (SVN revision 1203)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.5.3, released 2022/10/21
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rgdal/gdal
##  GDAL does not use iconv for recoding strings.
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 9.1.0, September 1st, 2022, [PJ_VERSION: 910]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.6-1
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(grid)
#Api key
tidycensus::census_api_key('b5977f1cc24b3460cb08c5d8c0010e1511ca9232',install = TRUE,overwrite=TRUE)
## Your original .Renviron will be backed up and stored in your R HOME directory if needed.
## Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). 
## To use now, restart R or run `readRenviron("~/.Renviron")`
## [1] "b5977f1cc24b3460cb08c5d8c0010e1511ca9232"
yelp_api='I2uZUuoRmSw0VQQ3HtjX2ji6ITgBucJCZaAPYMG3GwZR89UZ0EXIqBA-H1YMspmp50hRdMtOmBg6RnLb2kvOzmo6AfRBIVLV5pUjcojIx_psiTOajAiw0d5JpqH3ZHYx'

#Choose a State=GA, and a County in this State=Clayton
#Get Census Tract polygons

tract <- suppressMessages(
  get_acs(geography = "tract", # or "block group", "county", "state" etc. 
          state = "GA",
          county = c("Clayton"), 
          variables = c(hhincome = 'B19019_001',
                        race.tot = "B02001_001", 
                        race.white = "B02001_002", 
                        race.black = 'B02001_003'
          ),
          year = 2021,
          survey = "acs5", # American Community Survey 5-year estimate
          geometry = TRUE, # returns sf objects
          output = "wide") # wide vs. long
)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |======================================================================| 100%
#show the boundary in the map
tm_shape(tract) +  tm_borders(col = 'black')

##02 Create Census Tract Boundary for Yelp API In this step, we create the census tract boundary for Yelp API and visualize the buffer area.

# Function: Get tract-wise radius
get_r <- function(poly, epsg_id){
  # Get bounding box of a given polygon
  bb <- st_bbox(poly)
  # Get lat & long coordinates of any one corner of the bounding box.
  bb_corner <- st_point(c(bb[1], bb[2])) %>% st_sfc(crs = epsg_id)
  # Get centroid of the bb
  bb_center_x <- (bb[3]+bb[1])/2
  bb_center_y <- (bb[4]+bb[2])/2
  bb_center <- st_point(c(bb_center_x, bb_center_y)) %>% st_sfc(crs = epsg_id) %>% st_sf()
  
  # Get the distance between bb_p and c
  r <- st_distance(bb_corner, bb_center)
  # Multiply 1.1 to make the circle a bit larger than the Census Tract.
  # See the Yelp explanation of their radius parameter to see why we do this.
  bb_center$radius <- r*1.2
  return(bb_center)
}
# Creating an empty vector of NA
epsg_id <- 4326
r4all_loop <- vector("list", nrow(tract))
# Starting a for-loop
for (i in 1:nrow(tract)){
  r4all_loop[[i]] <- tract %>% 
    st_transform(crs = epsg_id) %>% 
    st_geometry() %>% 
    .[[i]] %>% 
    get_r(epsg_id = epsg_id)
}

r4all_loop <- bind_rows(r4all_loop)

# Appending X Y coordinates as seprate columns
ready_4_yelp <- r4all_loop %>% 
  mutate(x = st_coordinates(.)[,1],
         y = st_coordinates(.)[,2])
#visualization
 #set tmap mode to interactive viewing
tmap_mode('view')
## tmap mode set to interactive viewing
# Show all the result
ready_4_yelp[1:nrow(tract),] %>% 
  # Draw a buffer centered at the centroid of Tract polygons.
  # Radius of the buffer is the radius we just calculated using loop
  st_buffer(., dist = .$radius) %>% 
  # Display this buffer in red
  tm_shape(.) + tm_polygons(alpha = 0.5, col = 'red') +
  # Display the original polygon in blue
  tm_shape(tract) + tm_borders(col= 'blue')

##03 Access yelp API-test for two business types We define a function for accessing Yelp API, and then we use the function to get data for two business type “restaurants” and “bars”.

# Define a function for accessing Yelp API
get_yelp <- function(tract, category){
  # ----------------------------------
  # Gets one row of tract information (1,) and category name (str),
  # Outputs a list of business data.frame
  Sys.sleep(1)
  n <- 1
  # First request --------------------------------------------------------------
  resp <- business_search(api_key = yelp_api, 
                          categories = category, 
                          latitude = tract$y, 
                          longitude = tract$x, 
                          offset = (n - 1) * 50, # = 0 when n = 1
                          radius = round(tract$radius), 
                          limit = 50)
  # Calculate how many requests are needed in total
  required_n <- ceiling(resp$total/50)
  
  # out is where the results will be appended to.
  out <- vector("list", required_n)
  
  # Store the business information to nth slot in out
  out[[n]] <- resp$businesses
  
  # Change the name of the elements to the total required_n
  # This is to know if there are more than 1000 businesses,
  # we know how many.
  names(out)[n] <- required_n
  
  # Throw error if more than 1000
  if (resp$total >= 1000)
  {
    # glue formats string by inserting {n} with what's currently stored in object n.
    print(glue::glue("{n}th row has >= 1000 businesses."))
    # Stop before going into the loop because we need to
    # break down Census Tract to something smaller.
    return(out)
  } 
  else 
  {
    # add 1 to n
    n <- n + 1
    
    # Now we know required_n -----------------------------------------------------
    # Starting a loop
    while(n <= required_n){
      resp <- business_search(api_key = Sys.getenv("yelp_api"), 
                              categories = category, 
                              latitude = tract$y, 
                              longitude = tract$x, 
                              offset = (n - 1) * 50, 
                              radius = round(tract$radius), 
                              limit = 50)
      
      out[[n]] <- resp$businesses
      
      n <- n + 1
    } #<< end of while loop
    
    # Merge all elements in the list into a single data frame
    out <- out %>% bind_rows()
    
    return(out)
  }
}

###03-a: Try “restaurants”

#03-a:try "restaurants"
# Prepare a collector
yelp_restaurants_list <- vector("list", nrow(ready_4_yelp))

# Looping through all Census Tracts
for (row in 1:nrow(ready_4_yelp)){
  yelp_restaurants_list[[row]] <- suppressMessages(get_yelp(ready_4_yelp[row,], "restaurants"))
  if (row %% 10 == 0){
    print(paste0("Current row: ", row))
  }
}
## [1] "Current row: 10"
## [1] "Current row: 20"
## [1] "Current row: 30"
## [1] "Current row: 40"
## [1] "Current row: 50"
## [1] "Current row: 60"
## [1] "Current row: 70"

###03-b: Try “bars”

# 03-b: Try "bars"
# Prepare a collector
yelp_bars_list <- vector("list", nrow(ready_4_yelp))

# Looping through all Census Tracts
for (row in 1:nrow(ready_4_yelp)){
  yelp_bars_list[[row]] <- suppressMessages(get_yelp(ready_4_yelp[row,], "bars"))
  if (row %% 10 == 0){
    print(paste0("Current row: ", row))
  }
}
## [1] "Current row: 10"
## [1] "Current row: 20"
## [1] "Current row: 30"
## [1] "Current row: 40"
## [1] "Current row: 50"
## [1] "Current row: 60"
## [1] "Current row: 70"

###Collapsing the data

# Collapsing the list into a data.frame
yelp_restaurants <- yelp_restaurants_list %>% bind_rows() %>% as_tibble()

#calculate the number
paste0(" how many restaurants business?: ", nrow(yelp_restaurants))
## [1] " how many restaurants business?: 2597"
# print
yelp_restaurants %>% print(width=1000)
## # A tibble: 2,597 × 16
##    id                     alias                                       
##    <chr>                  <chr>                                       
##  1 4Wqj_yFOQj_ecnxKcn8aAg vn-pho-morrow                               
##  2 t4XdrDAbx5_9Yb1uLX77mA envegan-morrow                              
##  3 Jw_Drfjndv8e_wp-GY_TLg carrabbas-italian-grill-morrow-2            
##  4 VIr5uEwR86YXjViV60myCg mi-mexico-tienda-y-taqueria-morrow          
##  5 XfKNz-mlaQz7d9Ic0RDXvQ eggs-up-grill-morrow                        
##  6 J2osdfQe-8Wa6_rsO6BbRQ atlanta-philly-italian-pasta-and-subs-morrow
##  7 RRTA8rRQY--kIr-gSMRs3A eat-more-korean-morrow-4                    
##  8 _EeXiC61MQAGnpyAn-aGnw king-claw-juicy-seafood-and-bar-morrow      
##  9 X7knQxrMdUEnCe4AR4BR8A greedymans-bar-b-que-and-grill-jonesboro-2  
## 10 M39hcqI3xXcKsq6LwjReSw mr-cow-morrow                               
##    name                               
##    <chr>                              
##  1 VN Pho                             
##  2 envegan                            
##  3 Carrabba's Italian Grill           
##  4 Mi Mexico Tienda y Taqueria        
##  5 Eggs Up Grill                      
##  6 Atlanta Philly Italian Pasta & Subs
##  7 Eat More Korean                    
##  8 King Claw - Juicy Seafood & Bar    
##  9 Greedyman's Bar B Que & Grill      
## 10 Mr Cow                             
##    image_url                                                           
##    <chr>                                                               
##  1 https://s3-media4.fl.yelpcdn.com/bphoto/p0inrlSsokxb4GQ8ihLtQw/o.jpg
##  2 https://s3-media3.fl.yelpcdn.com/bphoto/2kj-7AckAs0Wj6V5hH6mKg/o.jpg
##  3 https://s3-media2.fl.yelpcdn.com/bphoto/xy8xa3S8_qVDIJnTJA_Eow/o.jpg
##  4 https://s3-media2.fl.yelpcdn.com/bphoto/M0KHcwRvJN71I8Gd5_lhxA/o.jpg
##  5 https://s3-media2.fl.yelpcdn.com/bphoto/mOoqjjSL3SrfPISL_A4gJg/o.jpg
##  6 https://s3-media1.fl.yelpcdn.com/bphoto/MID73bSYKNB_vFPQYCdg4A/o.jpg
##  7 https://s3-media3.fl.yelpcdn.com/bphoto/tsR1rx-_KAR4OcU2PbxDTQ/o.jpg
##  8 https://s3-media2.fl.yelpcdn.com/bphoto/Zz9tJaGBYa8jufCSBuQMfg/o.jpg
##  9 https://s3-media3.fl.yelpcdn.com/bphoto/uW9G-WTfMPZyz0AFXK0R_Q/o.jpg
## 10 https://s3-media2.fl.yelpcdn.com/bphoto/wKOmKuqLxvPHR3tvvSgyKw/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/vn-pho-morrow?adjust_creative=TrPUH-mrbq6wfryDSWK9R…
##  2 https://www.yelp.com/biz/envegan-morrow?adjust_creative=TrPUH-mrbq6wfryDSWK9…
##  3 https://www.yelp.com/biz/carrabbas-italian-grill-morrow-2?adjust_creative=Tr…
##  4 https://www.yelp.com/biz/mi-mexico-tienda-y-taqueria-morrow?adjust_creative=…
##  5 https://www.yelp.com/biz/eggs-up-grill-morrow?adjust_creative=TrPUH-mrbq6wfr…
##  6 https://www.yelp.com/biz/atlanta-philly-italian-pasta-and-subs-morrow?adjust…
##  7 https://www.yelp.com/biz/eat-more-korean-morrow-4?adjust_creative=TrPUH-mrbq…
##  8 https://www.yelp.com/biz/king-claw-juicy-seafood-and-bar-morrow?adjust_creat…
##  9 https://www.yelp.com/biz/greedymans-bar-b-que-and-grill-jonesboro-2?adjust_c…
## 10 https://www.yelp.com/biz/mr-cow-morrow?adjust_creative=TrPUH-mrbq6wfryDSWK9R…
##    review_count categories   rating coordinates$latitude $longitude transactions
##           <int> <list>        <dbl>                <dbl>      <dbl> <list>      
##  1          279 <df [1 × 2]>    4                   33.6      -84.3 <chr [2]>   
##  2          291 <df [1 × 2]>    4.5                 33.6      -84.3 <chr [2]>   
##  3          211 <df [2 × 2]>    3.5                 33.6      -84.3 <chr [2]>   
##  4           17 <df [3 × 2]>    5                   33.6      -84.3 <chr [0]>   
##  5           75 <df [3 × 2]>    3.5                 33.6      -84.3 <chr [2]>   
##  6           58 <df [3 × 2]>    4.5                 33.6      -84.3 <chr [2]>   
##  7          111 <df [3 × 2]>    4.5                 33.6      -84.3 <chr [2]>   
##  8           34 <df [2 × 2]>    4                   33.6      -84.3 <chr [2]>   
##  9           28 <df [3 × 2]>    5                   33.5      -84.3 <chr [2]>   
## 10           11 <df [2 × 2]>    5                   33.6      -84.3 <chr [0]>   
##    price location$address1   $address2 $address3 $city     $zip_code $country
##    <chr> <chr>               <chr>     <chr>     <chr>     <chr>     <chr>   
##  1 $$    6363 Jonesboro Rd    <NA>     <NA>      Morrow    30260     US      
##  2 $     1626 Lake Harbin Rd ""        <NA>      Morrow    30260     US      
##  3 $$    1887 Mt Zion Rd     ""        ""        Morrow    30260     US      
##  4 <NA>  6777 Mt Zion Blvd    <NA>     <NA>      Morrow    30260     US      
##  5 $$    1990 Mt Zion Rd      <NA>     ""        Morrow    30260     US      
##  6 <NA>  1510 Southlake Pkwy "Ste 1E"  <NA>      Morrow    30260     US      
##  7 $     1940 Mt Zion Rd      <NA>     <NA>      Morrow    30260     US      
##  8 <NA>  1965 Mt Zion Rd      <NA>     ""        Morrow    30260     US      
##  9 $$    1423 Stockbridge Rd  <NA>     ""        Jonesboro 30236     US      
## 10 <NA>  1940 Mt Zion Rd     ""        <NA>      Morrow    30260     US      
##    $state $display_address phone          display_phone    distance
##    <chr>  <list>           <chr>          <chr>               <dbl>
##  1 GA     <chr [2]>        "+17709606699" "(770) 960-6699"    1911.
##  2 GA     <chr [2]>        "+16785454242" "(678) 545-4242"    1698.
##  3 GA     <chr [2]>        "+17709683233" "(770) 968-3233"    1015.
##  4 GA     <chr [2]>        "+16785192699" "(678) 519-2699"     926.
##  5 GA     <chr [2]>        "+14702782917" "(470) 278-2917"    1116.
##  6 GA     <chr [3]>        "+16784897582" "(678) 489-7582"    1643.
##  7 GA     <chr [2]>        "+16788379577" "(678) 837-9577"     831.
##  8 GA     <chr [2]>        "+17704720024" "(770) 472-0024"    1177.
##  9 GA     <chr [2]>        "+18444733397" "(844) 473-3397"    5490.
## 10 GA     <chr [2]>        ""             ""                   831.
## # ℹ 2,587 more rows
# Collapsing the list into a data.frame
yelp_bars <- yelp_bars_list %>% bind_rows() %>% as_tibble()

#culculate the number
paste0(" how many bars business?: ", nrow(yelp_bars))
## [1] " how many bars business?: 360"
# print
yelp_bars %>% print(width=1000)
## # A tibble: 360 × 16
##    id                     alias                                   
##    <chr>                  <chr>                                   
##  1 4rgCKAEyj4_SUlh6YG-krw red-room-hookah-lounge-college-park     
##  2 OHjWsZV5XuH8Zb77C76V5w savoy-bistro-morrow-2                   
##  3 J7BzvyHHrbxJFLxQyfTADQ tgi-fridays-morrow                      
##  4 F55BAybX9TyIJe2FnqkivA skyboxx-restaurant-and-sports-bar-morrow
##  5 V4moL5wTgnUXGTvhvZM3dg rodeo-sports-bar-morrow                 
##  6 CX_yxk6GICxopny0uAX5Ew chilis-morrow-2                         
##  7 kQmgNLFj7ujsxr5xmFQzbA cru-lounge-morrow-morrow                
##  8 im-juAHqpbUsF2yXvF3jdQ the-b-spot-morrow                       
##  9 RuzLTI0uYh6Kn8uxnCoEuQ bohío-restaurant-and-lounge-morrow      
## 10 iMzO01QSapoEaNfQX4JOBQ myxers-bar-and-grill-jonesboro          
##    name                             
##    <chr>                            
##  1 Red Room Hookah Lounge           
##  2 Savoy Bistro                     
##  3 TGI Fridays                      
##  4 Skyboxx Restaurant and Sports Bar
##  5 Rodeo Sports Bar                 
##  6 Chili's                          
##  7 CRU Lounge Morrow                
##  8 The B Spot                       
##  9 Bohío Restaurant and Lounge      
## 10 Myxers Bar and Grill             
##    image_url                                                           
##    <chr>                                                               
##  1 https://s3-media3.fl.yelpcdn.com/bphoto/qLcAVc31N89ENJVaOdJdzA/o.jpg
##  2 https://s3-media1.fl.yelpcdn.com/bphoto/UHUr1Qhk_jXmczQq19CWdw/o.jpg
##  3 https://s3-media2.fl.yelpcdn.com/bphoto/z2ylCdwQ8ZvAajtC2tS6bA/o.jpg
##  4 https://s3-media4.fl.yelpcdn.com/bphoto/uikgrv_i1XTC84y6fzr65Q/o.jpg
##  5 https://s3-media3.fl.yelpcdn.com/bphoto/9E8I_R6toHNUdIIrbJfKTA/o.jpg
##  6 https://s3-media4.fl.yelpcdn.com/bphoto/Rr86-bNKkd_bAGixLLsb3g/o.jpg
##  7 https://s3-media4.fl.yelpcdn.com/bphoto/2u-CpuBkJyPPjFk2kaIy8g/o.jpg
##  8 https://s3-media2.fl.yelpcdn.com/bphoto/ggiTGSg2AdIhKIsdFVq6Zw/o.jpg
##  9 https://s3-media2.fl.yelpcdn.com/bphoto/ZT3od-02phePWDBXB883Ug/o.jpg
## 10 https://s3-media2.fl.yelpcdn.com/bphoto/Fabqlut9afxjOTdbtaVJvw/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/red-room-hookah-lounge-college-park?adjust_creative…
##  2 https://www.yelp.com/biz/savoy-bistro-morrow-2?adjust_creative=TrPUH-mrbq6wf…
##  3 https://www.yelp.com/biz/tgi-fridays-morrow?adjust_creative=TrPUH-mrbq6wfryD…
##  4 https://www.yelp.com/biz/skyboxx-restaurant-and-sports-bar-morrow?adjust_cre…
##  5 https://www.yelp.com/biz/rodeo-sports-bar-morrow?adjust_creative=TrPUH-mrbq6…
##  6 https://www.yelp.com/biz/chilis-morrow-2?adjust_creative=TrPUH-mrbq6wfryDSWK…
##  7 https://www.yelp.com/biz/cru-lounge-morrow-morrow?adjust_creative=TrPUH-mrbq…
##  8 https://www.yelp.com/biz/the-b-spot-morrow?adjust_creative=TrPUH-mrbq6wfryDS…
##  9 https://www.yelp.com/biz/boh%C3%ADo-restaurant-and-lounge-morrow?adjust_crea…
## 10 https://www.yelp.com/biz/myxers-bar-and-grill-jonesboro?adjust_creative=TrPU…
##    review_count categories   rating coordinates$latitude $longitude transactions
##           <int> <list>        <dbl>                <dbl>      <dbl> <list>      
##  1            2 <df [1 × 2]>    4                   33.6      -84.4 <chr [2]>   
##  2          123 <df [3 × 2]>    3.5                 33.6      -84.3 <chr [2]>   
##  3          161 <df [3 × 2]>    2                   33.6      -84.3 <chr [3]>   
##  4          138 <df [3 × 2]>    2                   33.6      -84.3 <chr [2]>   
##  5           90 <df [1 × 2]>    2.5                 33.6      -84.3 <chr [1]>   
##  6          116 <df [3 × 2]>    2                   33.6      -84.3 <chr [2]>   
##  7            1 <df [3 × 2]>    5                   33.6      -84.3 <chr [0]>   
##  8           10 <df [3 × 2]>    3                   33.6      -84.3 <chr [0]>   
##  9            2 <df [3 × 2]>    5                   33.6      -84.3 <chr [0]>   
## 10            1 <df [3 × 2]>    1                   33.6      -84.3 <chr [2]>   
##    location$address1    $address2  $address3 $city        $zip_code $country
##    <chr>                <chr>      <chr>     <chr>        <chr>     <chr>   
##  1 571 Garden Walk Blvd  <NA>      <NA>      College Park 30349     US      
##  2 7045 Mount Zion Cir   <NA>      ""        Morrow       30260     US      
##  3 1881 Mount Zion Rd   ""         ""        Morrow       30260     US      
##  4 2180 Mount Zion Pkwy ""         ""        Morrow       30236     US      
##  5 1869 Mt Zion Rd      ""         <NA>      Morrow       30260     US      
##  6 2230 Mount Zion Pkwy ""         ""        Morrow       30260     US      
##  7 1905 Mt Zion Rd      "Ste V 25" ""        Morrow       30260     US      
##  8 6335 Jonesboro Rd    "Ste A"    ""        Morrow       30260     US      
##  9 1395 Southlake Pkwy   <NA>      ""        Morrow       30260     US      
## 10 2745 Mt Zion Rd       <NA>      ""        Jonesboro    30236     US      
##    $state $display_address phone          display_phone    distance price
##    <chr>  <list>           <chr>          <chr>               <dbl> <chr>
##  1 GA     <chr [2]>        "+16786076733" "(678) 607-6733"     698. <NA> 
##  2 GA     <chr [2]>        "+17708925238" "(770) 892-5238"    1164. $$   
##  3 GA     <chr [2]>        "+17709683303" "(770) 968-3303"    1020. $$   
##  4 GA     <chr [2]>        "+17704719191" "(770) 471-9191"    1332. $$   
##  5 GA     <chr [2]>        "+17709687434" "(770) 968-7434"    1029. $$   
##  6 GA     <chr [2]>        "+17706039900" "(770) 603-9900"    1376. $$   
##  7 GA     <chr [3]>        ""             ""                  1285. <NA> 
##  8 GA     <chr [3]>        "+16785451185" "(678) 545-1185"    1958. <NA> 
##  9 GA     <chr [2]>        "+17707031799" "(770) 703-1799"    2229. <NA> 
## 10 GA     <chr [2]>        "+16785865925" "(678) 586-5925"    2746. <NA> 
## # ℹ 350 more rows
# Collapsing the list into a data.frame
yelp_all <- bind_rows(yelp_bars,yelp_restaurants) %>% as_tibble()

#culculate the number
paste0(" how many business?: ", nrow(yelp_all))
## [1] " how many business?: 2957"
# print
yelp_all %>% print(width=1000)
## # A tibble: 2,957 × 16
##    id                     alias                                   
##    <chr>                  <chr>                                   
##  1 4rgCKAEyj4_SUlh6YG-krw red-room-hookah-lounge-college-park     
##  2 OHjWsZV5XuH8Zb77C76V5w savoy-bistro-morrow-2                   
##  3 J7BzvyHHrbxJFLxQyfTADQ tgi-fridays-morrow                      
##  4 F55BAybX9TyIJe2FnqkivA skyboxx-restaurant-and-sports-bar-morrow
##  5 V4moL5wTgnUXGTvhvZM3dg rodeo-sports-bar-morrow                 
##  6 CX_yxk6GICxopny0uAX5Ew chilis-morrow-2                         
##  7 kQmgNLFj7ujsxr5xmFQzbA cru-lounge-morrow-morrow                
##  8 im-juAHqpbUsF2yXvF3jdQ the-b-spot-morrow                       
##  9 RuzLTI0uYh6Kn8uxnCoEuQ bohío-restaurant-and-lounge-morrow      
## 10 iMzO01QSapoEaNfQX4JOBQ myxers-bar-and-grill-jonesboro          
##    name                             
##    <chr>                            
##  1 Red Room Hookah Lounge           
##  2 Savoy Bistro                     
##  3 TGI Fridays                      
##  4 Skyboxx Restaurant and Sports Bar
##  5 Rodeo Sports Bar                 
##  6 Chili's                          
##  7 CRU Lounge Morrow                
##  8 The B Spot                       
##  9 Bohío Restaurant and Lounge      
## 10 Myxers Bar and Grill             
##    image_url                                                           
##    <chr>                                                               
##  1 https://s3-media3.fl.yelpcdn.com/bphoto/qLcAVc31N89ENJVaOdJdzA/o.jpg
##  2 https://s3-media1.fl.yelpcdn.com/bphoto/UHUr1Qhk_jXmczQq19CWdw/o.jpg
##  3 https://s3-media2.fl.yelpcdn.com/bphoto/z2ylCdwQ8ZvAajtC2tS6bA/o.jpg
##  4 https://s3-media4.fl.yelpcdn.com/bphoto/uikgrv_i1XTC84y6fzr65Q/o.jpg
##  5 https://s3-media3.fl.yelpcdn.com/bphoto/9E8I_R6toHNUdIIrbJfKTA/o.jpg
##  6 https://s3-media4.fl.yelpcdn.com/bphoto/Rr86-bNKkd_bAGixLLsb3g/o.jpg
##  7 https://s3-media4.fl.yelpcdn.com/bphoto/2u-CpuBkJyPPjFk2kaIy8g/o.jpg
##  8 https://s3-media2.fl.yelpcdn.com/bphoto/ggiTGSg2AdIhKIsdFVq6Zw/o.jpg
##  9 https://s3-media2.fl.yelpcdn.com/bphoto/ZT3od-02phePWDBXB883Ug/o.jpg
## 10 https://s3-media2.fl.yelpcdn.com/bphoto/Fabqlut9afxjOTdbtaVJvw/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/red-room-hookah-lounge-college-park?adjust_creative…
##  2 https://www.yelp.com/biz/savoy-bistro-morrow-2?adjust_creative=TrPUH-mrbq6wf…
##  3 https://www.yelp.com/biz/tgi-fridays-morrow?adjust_creative=TrPUH-mrbq6wfryD…
##  4 https://www.yelp.com/biz/skyboxx-restaurant-and-sports-bar-morrow?adjust_cre…
##  5 https://www.yelp.com/biz/rodeo-sports-bar-morrow?adjust_creative=TrPUH-mrbq6…
##  6 https://www.yelp.com/biz/chilis-morrow-2?adjust_creative=TrPUH-mrbq6wfryDSWK…
##  7 https://www.yelp.com/biz/cru-lounge-morrow-morrow?adjust_creative=TrPUH-mrbq…
##  8 https://www.yelp.com/biz/the-b-spot-morrow?adjust_creative=TrPUH-mrbq6wfryDS…
##  9 https://www.yelp.com/biz/boh%C3%ADo-restaurant-and-lounge-morrow?adjust_crea…
## 10 https://www.yelp.com/biz/myxers-bar-and-grill-jonesboro?adjust_creative=TrPU…
##    review_count categories   rating coordinates$latitude $longitude transactions
##           <int> <list>        <dbl>                <dbl>      <dbl> <list>      
##  1            2 <df [1 × 2]>    4                   33.6      -84.4 <chr [2]>   
##  2          123 <df [3 × 2]>    3.5                 33.6      -84.3 <chr [2]>   
##  3          161 <df [3 × 2]>    2                   33.6      -84.3 <chr [3]>   
##  4          138 <df [3 × 2]>    2                   33.6      -84.3 <chr [2]>   
##  5           90 <df [1 × 2]>    2.5                 33.6      -84.3 <chr [1]>   
##  6          116 <df [3 × 2]>    2                   33.6      -84.3 <chr [2]>   
##  7            1 <df [3 × 2]>    5                   33.6      -84.3 <chr [0]>   
##  8           10 <df [3 × 2]>    3                   33.6      -84.3 <chr [0]>   
##  9            2 <df [3 × 2]>    5                   33.6      -84.3 <chr [0]>   
## 10            1 <df [3 × 2]>    1                   33.6      -84.3 <chr [2]>   
##    location$address1    $address2  $address3 $city        $zip_code $country
##    <chr>                <chr>      <chr>     <chr>        <chr>     <chr>   
##  1 571 Garden Walk Blvd  <NA>      <NA>      College Park 30349     US      
##  2 7045 Mount Zion Cir   <NA>      ""        Morrow       30260     US      
##  3 1881 Mount Zion Rd   ""         ""        Morrow       30260     US      
##  4 2180 Mount Zion Pkwy ""         ""        Morrow       30236     US      
##  5 1869 Mt Zion Rd      ""         <NA>      Morrow       30260     US      
##  6 2230 Mount Zion Pkwy ""         ""        Morrow       30260     US      
##  7 1905 Mt Zion Rd      "Ste V 25" ""        Morrow       30260     US      
##  8 6335 Jonesboro Rd    "Ste A"    ""        Morrow       30260     US      
##  9 1395 Southlake Pkwy   <NA>      ""        Morrow       30260     US      
## 10 2745 Mt Zion Rd       <NA>      ""        Jonesboro    30236     US      
##    $state $display_address phone          display_phone    distance price
##    <chr>  <list>           <chr>          <chr>               <dbl> <chr>
##  1 GA     <chr [2]>        "+16786076733" "(678) 607-6733"     698. <NA> 
##  2 GA     <chr [2]>        "+17708925238" "(770) 892-5238"    1164. $$   
##  3 GA     <chr [2]>        "+17709683303" "(770) 968-3303"    1020. $$   
##  4 GA     <chr [2]>        "+17704719191" "(770) 471-9191"    1332. $$   
##  5 GA     <chr [2]>        "+17709687434" "(770) 968-7434"    1029. $$   
##  6 GA     <chr [2]>        "+17706039900" "(770) 603-9900"    1376. $$   
##  7 GA     <chr [3]>        ""             ""                  1285. <NA> 
##  8 GA     <chr [3]>        "+16785451185" "(678) 545-1185"    1958. <NA> 
##  9 GA     <chr [2]>        "+17707031799" "(770) 703-1799"    2229. <NA> 
## 10 GA     <chr [2]>        "+16785865925" "(678) 586-5925"    2746. <NA> 
## # ℹ 2,947 more rows

##04 Map the Data

# Extract coordinates
yelp_sf <- yelp_all %>% 
  mutate(x = .$coordinates$longitude,
         y = .$coordinates$latitude) %>% 
  filter(!is.na(x) & !is.na(y)) %>% 
  st_as_sf(coords = c("x", "y"), crs = 4326)

# Map
tm_shape(yelp_sf) +
  tm_dots(col = "review_count", style="quantile")

##05 Answer the Questions:

###-What’s the county and state of your choice? Clayton, GA
###-How many businesses are there in total?
There are 2957 businesses in total.
###-How many businesses are there for each business category? For restaurants, there are 2597 businesses.
For bars, there are 360 businesses.
###-Upon visual inspection, can you see any noticeable spatial patterns to the way they are distributed across the county (e.g., clustering of businesses at some parts of the county)? *Cluster of restaurants and bars are concentrated in the northwest of Clayton County.