In Class exercise 12

Analysis
R
sf
tidyverse
tmap
httr
performance
Author

Brian Lim

Published

November 4, 2024

Modified

November 4, 2024

Loading the R packages

pacman::p_load(tidyverse, sf, tmap, httr, performance)

The code chunk below imports multiple csv files in a specified folder and append them into a single tibble data frame.

folder_path <- "data/In-class_Ex12/aspatial"
file_list <- list.files(path = folder_path, 
                        pattern = "^realis.*\\.csv$", 
                        full.names = TRUE)

realis_data <- file_list %>%
  map_dfr(read_csv)

The following code chunk converts values in Sale Date field from character to numerical date format, and - extracting resale and condominium transaction records.

condo_resale <- realis_data %>%
  mutate(`Sale Date` = dmy(`Sale Date`)) %>%
  filter(`Type of Sale` == "Resale" &
           `Property Type` == "Condominium")

Data preparation

postcode <- unique(condo_resale$`Postal Code`)
url <- "https://onemap.gov.sg/api/common/elastic/search"
found <- data.frame()
not_found <- data.frame()

for (postcode in postcode){
  query <- list('searchVal'=postcode, 'returnGeom'='Y', 
                'getAddrDetails'='Y', 'pageNum'='1')
  res <- GET(url, query=query)
  if ((content(res)$found)!=0){
    found <- rbind(found, data.frame(content(res))[4:13])
  } else {not_found = data.frame(postcode)
  }
}

Tidying up field names

found <- found %>%
  select(c(6:8)) %>%
  rename(POSTAL = `results.POSTAL`,
         XCOORD = `results.X`,
         YCOORD = `results.Y`)
write_rds(found, "data/In-class_Ex12/found.rds")
found <- read_rds("data/In-class_Ex12/found.rds")

The following chunk of code is to join condo_resale and found

condo_resale_geocoded = left_join(
  condo_resale, found, 
  by = c('Postal Code' = 'POSTAL'))

The following chunk of code is to convert condo_resale_geocoded from tibble data frame to sf point feature data frame

condo_resale_sf <- st_as_sf(condo_resale_geocoded, 
                            coords = c("XCOORD",
                                       "YCOORD"),
                            crs=3414)

Cleaning spatial data

The code chunk below is used to check if there are overlapping point features.

overlapping_points <- condo_resale_sf %>%
  mutate(overlap = lengths(st_equals(., .)) > 1)

In the code code chunk below, st_jitter() of sf package is used to move the point features by 5m to avoid overlapping point features.

condo_resale_sf <- condo_resale_sf %>%
  st_jitter(amount = 2)