Setup Libraries and API Key
tidycensus::census_api_key(Sys.getenv("census_api"))
## To install your API key for use in future sessions, run this function with `install = TRUE`.
STEP 1 & 2 I decide to choose restaurants and bars as the categories, and Williamsburg (VA) as the city
city <- "Williamsburg"
state <- "VA"
Step 3 Get Census Tracts data for the selected city
# Tract polygons for the Yelp query
tract <- suppressMessages(
get_acs(geography = "tract",
state = state,
county = c("James City"),
variables = c(hhincome = 'B19019_001'),
year = 2021,
survey = "acs5",
geometry = TRUE,
output = "wide")
)
# tigris is used here to get the city boundary
williamsburg <- tigris::places(state) %>%
filter(NAME == city)
## Retrieving data for the year 2022
tract_williamsburg <- tract[williamsburg,]
Step 4 R code to get business data from Yelp
# Function to get Yelp business data for a Census Tract
get_yelp <- function(tract, category) {
api_key <- Sys.getenv("yelp_api")
centroid <- st_centroid(tract$geometry)
latitude <- st_coordinates(centroid)[1, 2]
longitude <- st_coordinates(centroid)[1, 1]
resp <- business_search(api_key = api_key, # Using yelpr
categories = category,
latitude = latitude,
longitude = longitude,
limit = 50)
return(resp$businesses)
}
# Collect Yelp data for two different business categories
yelp_first_category <- get_yelp(tract_williamsburg[1,], "restaurants") %>% as_tibble() %>% mutate(category = "restaurants")
## No encoding supplied: defaulting to UTF-8.
yelp_second_category <- get_yelp(tract_williamsburg[1,], "bars") %>% as_tibble() %>% mutate(category = "bars")
## No encoding supplied: defaulting to UTF-8.
# Merge the data for both categories into a single data frame as step 1 said
yelp_combined <- bind_rows(yelp_first_category, yelp_second_category)
Step 5 Create two maps
# Switch to interactive map mode
tmap_mode("view")
## tmap mode set to interactive viewing
# Map showing the city boundary and its Census Tracts
tm_shape(tract_williamsburg) +
tm_borders(lwd = 2) +
tm_shape(williamsburg) +
tm_polygons(col = 'red', alpha = 0.4)
# Map showing business locations from Yelp for both categories
yelp_combined <- yelp_combined %>%
mutate(x = .$coordinates$longitude,
y = .$coordinates$latitude) %>%
filter(!is.na(x) & !is.na(y)) %>%
st_as_sf(coords = c("x", "y"), crs = 4326)
tm_shape(yelp_combined) +
tm_dots(col = "review_count", style = "quantile")
save the data in the end
save(yelp_combined, file = "yelp_data_williamsburg.RData")
Step 6 Answers to the questions
1.Which city did you choose?
# Print the chosen city
cat("The city chosen is:", city)
## The city chosen is: Williamsburg
2.How many businesses are there in total?
# Count total number of businesses
total_businesses <- nrow(yelp_combined)
cat("Total number of businesses:", total_businesses)
## Total number of businesses: 100
3.How many businesses are there for each business category?
businesses_by_category <- yelp_combined %>%
group_by(category) %>%
summarise(count = n())
# Display the counts
print(businesses_by_category)
## Simple feature collection with 2 features and 2 fields
## Geometry type: MULTIPOINT
## Dimension: XY
## Bounding box: xmin: -76.75862 ymin: 37.23603 xmax: -76.64509 ymax: 37.3281
## Geodetic CRS: WGS 84
## # A tibble: 2 × 3
## category count geometry
## <chr> <int> <MULTIPOINT [°]>
## 1 bars 50 ((-76.64509 37.23686), (-76.64757 37.23603), (-76.66061 37.…
## 2 restaurants 50 ((-76.68554 37.24923), (-76.66061 37.24848), (-76.65868 37.…
4.Upon visual inspection, can you see any noticeable spatial patterns to the way they are distributed across the city (e.g., clustering of businesses at some parts of the city)? There appears to be a noticeable clustering of businesses in the central parts of the city, particularly around the downtown areas, as well as the surrounding neighborhoods. These areas likely attract higher foot traffic and are central hubs for dining and commerce. In contrast, there are fewer businesses located in the more peripheral areas, with many of them distributed near major highways/major roads.
5.(Optional) Are there any other interesting findings? Since Williamsburg is a VERY small city (with a total population of around 15,000, more than half of whom are students or faculty from the College of William and Mary) and has a lot of green spaces, businesses are highly concentrated in a few areas along the main roads. You’ll find fewer than 10 commercial establishments in other areas. Compared to the other cities I explored with the data, Williamsburg’s business distribution is much more centralized/extreme. Most students and faculty tend to frequent these concentrated areas since they are located right next to the campus. P.S. It’s a really nice city—visit if you get a chance!