::p_load(sf, tmap, tidyverse,httr) pacman
In-class Exercise 3b : Working with Open Government Data
1 Load the R package
2 Import the ACRA data
<- "data/aspatial/ACRA"
folder_path <- list.files(path = folder_path,
file_list pattern = "^ACRA*.*\\.csv$",
full.names = TRUE)
<- file_list %>%
acra_data map_dfr(read_csv)
3 Saving the ACRA data
write_rds(acra_data,
"data/rds/acra_data.rds")
4 Tidying the ACRA data
<- acra_data %>%
biz_56111 select(1:24) %>%
filter(primary_ssic_code == 56111) %>%
rename(date = registration_incorporation_date) %>%
mutate(date = as.Date(date),
YEAR = year(date),
MONTH_NUM = month(date),
MONTH_ABBR = month(date,
label = TRUE,
abbr = TRUE)) %>%
mutate(
postal_code = str_pad(postal_code,
width = 6, side = "left", pad = "0")) %>%
filter(YEAR == 2025)
5 Geocoding
<- unique(biz_56111$postal_code)
postcodes
<- "https://onemap.gov.sg/api/common/elastic/search"
url
<-data.frame()
found <- data.frame(postcode = character())
not_found
for(pc in postcodes) {
<- list(
query searchVal = pc,
returnGeom = "Y",
getAddrDetails = "Y",
pageNum = "1"
)
<- GET(url,query = query)
res <- content(res)
json
if(json$found !=0) {
<- as.data.frame(json$results,stringAsFactors = FALSE)
df $input_postcode <- pc
df<- bind_rows(found,df)
found else {
} <- bind_rows(not_found,data,frame(postcode = pc))
not_found
} }
6 Tidying the geocoded data
<- found %>%
found select(1:10)
7 Appending the location information
= biz_56111 %>%
biz_56111 left_join(found,
by = c('postal_code' = 'POSTAL'))
8 Saving the data
write_rds(biz_56111, "data/rds/biz_56111.rds")
9 Converting into SF data frame
<- st_as_sf(biz_56111,
biz_56111_sf coords = c("X", "Y"),
crs = 3414)
10 Visualization of the distribution
ggplot(data = biz_56111,
aes(x = MONTH_ABBR)) +
geom_bar()
11 Visualizaing the business
tmap_mode('view')
tm_shape(biz_56111_sf)+
tm_dots()