In-class Ex 01

Published

November 18, 2023

Modified

December 16, 2023

Load R packages

The following code chunk loads the necessary packages:

  • tmap: for thematic mapping
  • sf: for geospatial data handling
  • tidyverse: for aspatial data transformation
pacman::p_load(tmap, sf, tidyverse)

Preparing the OD Data

Import the Passenger Volume by Origin Destination dataset downloaded from LTA DataMall using read_csv() of the readr package:

odbus08 <- read_csv("data/aspatial/origin_destination_bus_202308.csv")

# bus09 <- read_csv("data/aspatial/origin_destination_bus_202309.csv")
# bus10 <- read_csv("data/aspatial/origin_destination_bus_202310.csv")

odbus08 is a tibble dataframe. However, ORIGIN_PT_CODE and DESTINATION_PT_CODE are in character format. These are transformed into factors (categorical data type) for further analysis.

odbus08 <- odbus08 %>%
  mutate(
    ORIGIN_PT_CODE = as.factor(ORIGIN_PT_CODE),
    DESTINATION_PT_CODE = as.factor(DESTINATION_PT_CODE)
  )

Extracting the data for Analysis

Create a new dataframe origtrip_7_9 by extracting Origin busstop codes and number of trips for weekdays between 7 and 9 o’clock:

origtrip_7_9 <- odbus08 %>%
  filter(
    DAY_TYPE == "WEEKDAY"
  ) %>%
  filter(
    TIME_PER_HOUR >= 7 & TIME_PER_HOUR <= 9
  ) %>%
  group_by(
    ORIGIN_PT_CODE
  ) %>%
  summarise(
    TRIPS = sum(TOTAL_TRIPS)
  ) %>%
  ungroup()

Loading the geospatial data

busstop <- st_read(
    dsn = "data/geospatial",
    layer = "BusStop"
  ) %>%
  # Assigning the right EPSG code based on coordinate system
  st_transform(
    crs = 3414
  )
Reading layer `BusStop' from data source 
  `C:\haileycsy\ISSS624-AGA\In-class_Ex\ice1\data\geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 5161 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 3970.122 ymin: 26482.1 xmax: 48284.56 ymax: 52983.82
Projected CRS: SVY21
mpsz <- st_read(
    dsn = "data/geospatial",
    layer = "MPSZ-2019"
  ) %>%
 # Assigning the right EPSG code
  st_transform(
    crs = 3414
  )
Reading layer `MPSZ-2019' from data source 
  `C:\haileycsy\ISSS624-AGA\In-class_Ex\ice1\data\geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 332 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 103.6057 ymin: 1.158699 xmax: 104.0885 ymax: 1.470775
Geodetic CRS:  WGS 84