School of Computing and Information Systems,
Singapore Management University
2025-09-11
By the end of this hands-on exercise, you will be able to:
Write a code chunk to install and load tidyverse, sf, tmap and httr into R environment.
Note
httr is an R package specially designed to provide a wrapper for the curl package, customised to the demands of modern web APIs. Key features: Functions for the most important http verbs: GET(), HEAD(), PATCH(), PUT(), DELETE() and POST().
dplyr is a grammar of data manipulation, providing a consistent set of verbs that help you solve the most common data manipulation challenges. It is a must learned package for modern data scientists and data analysts. Refer to this chapter to learn more about dplyr.
lubridate is an R package specially designed to handle date and date-time data type. Refer to this chapter to learn more about handle date and datetime with lubridate.
Write a code chunk to perform the followings:
Write a code chunk to perform the followings:
biz_56111 <- acra_data %>%
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) Write a function to perform the following tasks:
postcodes <- unique(biz_56111$postal_code)
url <- "https://onemap.gov.sg/api/common/elastic/search"
found <- data.frame()
not_found <- data.frame(postcode = character())
for (pc in postcodes) {
query <- list(
searchVal = pc,
returnGeom = "Y",
getAddrDetails = "Y",
pageNum = "1"
)
res <- GET(url, query = query)
json <- content(res)
if (json$found != 0) {
df <- as.data.frame(json$results, stringsAsFactors = FALSE)
df$input_postcode <- pc
found <- bind_rows(found, df)
} else {
not_found <- bind_rows(not_found, data.frame(postcode = pc))
}
}