Import wide text file

Read in Sachi’s NYC Farmers Markets data that is in csv file.

farmers <- read_csv("https://raw.githubusercontent.com/jacshap/Data607/refs/heads/main/nyc_farmers_markets_visitors_wide.csv", col_names = TRUE, show_col_types = FALSE) 

knitr::kable(farmers)
Week Manhattan_Count Brooklyn_Count Queens_Count Bronx_Count StatenIsland_Count
2025-08-03 1842 2311 1097 856 402
2025-08-10 1915 2468 1121 903 417
2025-08-17 2004 2387 1180 891 439
2025-08-24 1962 2520 1145 934 455

Format to long

farmers <- farmers %>% pivot_longer(cols = 'Manhattan_Count':'StatenIsland_Count',
                               names_to = 'Borough',
                               values_to = "Count")
#clean up
farmers <- farmers %>% mutate(Borough = str_replace_all(Borough, c("_Count"="")))

knitr::kable(farmers)
Week Borough Count
2025-08-03 Manhattan 1842
2025-08-03 Brooklyn 2311
2025-08-03 Queens 1097
2025-08-03 Bronx 856
2025-08-03 StatenIsland 402
2025-08-10 Manhattan 1915
2025-08-10 Brooklyn 2468
2025-08-10 Queens 1121
2025-08-10 Bronx 903
2025-08-10 StatenIsland 417
2025-08-17 Manhattan 2004
2025-08-17 Brooklyn 2387
2025-08-17 Queens 1180
2025-08-17 Bronx 891
2025-08-17 StatenIsland 439
2025-08-24 Manhattan 1962
2025-08-24 Brooklyn 2520
2025-08-24 Queens 1145
2025-08-24 Bronx 934
2025-08-24 StatenIsland 455

Analysis

Will do 2 analyses suggested by Sachi 1. Compare week-over-week trends by borough (levels + % change). 2. Compute share of total visitors each week (who’s contributing most).

Week-over-week by borough

% change = (new value - old value)/old value * 100

perc_change <- farmers %>% group_by(Borough) %>% mutate(lagged_perc = lag(Count)) %>% mutate(percent_change = (Count - lagged_perc)/lagged_perc*100) %>% select(Week, Borough, Count, percent_change)

perc_change <- perc_change[-(1:5),] %>% arrange(Borough)

knitr::kable(perc_change)
Week Borough Count percent_change
2025-08-10 Bronx 903 5.490654
2025-08-17 Bronx 891 -1.328904
2025-08-24 Bronx 934 4.826038
2025-08-10 Brooklyn 2468 6.793596
2025-08-17 Brooklyn 2387 -3.282010
2025-08-24 Brooklyn 2520 5.571847
2025-08-10 Manhattan 1915 3.963084
2025-08-17 Manhattan 2004 4.647520
2025-08-24 Manhattan 1962 -2.095808
2025-08-10 Queens 1121 2.187785
2025-08-17 Queens 1180 5.263158
2025-08-24 Queens 1145 -2.966102
2025-08-10 StatenIsland 417 3.731343
2025-08-17 StatenIsland 439 5.275779
2025-08-24 StatenIsland 455 3.644647
ggplot(data=perc_change, aes(x=Week, y=percent_change, fill = Borough)) + 
  geom_bar(stat="identity", position = "dodge") +
  labs(title = "Percent Change per Borough", x = "Week", y = "Percent Change") + 
  theme_light()

Share of total visitors each week

week_sum <- farmers %>% group_by(Week) %>% mutate(Sum_Week = sum(Count))

Visitor_Split_Percentage <- week_sum %>% mutate(Visitor_Split = Count/Sum_Week*100)

knitr::kable(Visitor_Split_Percentage)
Week Borough Count Sum_Week Visitor_Split
2025-08-03 Manhattan 1842 6508 28.303626
2025-08-03 Brooklyn 2311 6508 35.510141
2025-08-03 Queens 1097 6508 16.856177
2025-08-03 Bronx 856 6508 13.153042
2025-08-03 StatenIsland 402 6508 6.177013
2025-08-10 Manhattan 1915 6824 28.062720
2025-08-10 Brooklyn 2468 6824 36.166471
2025-08-10 Queens 1121 6824 16.427315
2025-08-10 Bronx 903 6824 13.232708
2025-08-10 StatenIsland 417 6824 6.110786
2025-08-17 Manhattan 2004 6901 29.039270
2025-08-17 Brooklyn 2387 6901 34.589190
2025-08-17 Queens 1180 6901 17.098971
2025-08-17 Bronx 891 6901 12.911172
2025-08-17 StatenIsland 439 6901 6.361397
2025-08-24 Manhattan 1962 7016 27.964652
2025-08-24 Brooklyn 2520 7016 35.917902
2025-08-24 Queens 1145 7016 16.319840
2025-08-24 Bronx 934 7016 13.312429
2025-08-24 StatenIsland 455 7016 6.485177
ggplot(data=Visitor_Split_Percentage, aes(x=Week, y=Visitor_Split, fill = Borough)) + 
  geom_bar(stat="identity", position = "dodge") +
  labs(title = "Share of Visitors per Borough", x = "Week", y = "Percent of Visitors") + 
  theme_light()