Hands-On Exercise 2

Week 2 Hands-on Exercise

pacman::p_load(sf, tidyverse)
mpsz = st_read(dsn = "data/geospatial", 
                  layer = "MP14_SUBZONE_WEB_PL")
Reading layer `MP14_SUBZONE_WEB_PL' from data source 
  `/Users/michelle/Desktop/IS415/shelle-mim/IS415-GAA/Hands-on_Exercise/Wk2/data/geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 323 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21

Import polyline feature data as shapefile

cyclingpath = st_read(dsn = "data/geospatial", 
                         layer = "CyclingPathGazette")
Reading layer `CyclingPathGazette' from data source 
  `/Users/michelle/Desktop/IS415/shelle-mim/IS415-GAA/Hands-on_Exercise/Wk2/data/geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 2248 features and 2 fields
Geometry type: MULTILINESTRING
Dimension:     XY
Bounding box:  xmin: 11854.32 ymin: 28347.98 xmax: 42626.09 ymax: 48948.15
Projected CRS: SVY21
preschool = st_read("data/geospatial/preschools-location.kml")
Reading layer `PRESCHOOLS_LOCATION' from data source 
  `/Users/michelle/Desktop/IS415/shelle-mim/IS415-GAA/Hands-on_Exercise/Wk2/data/geospatial/preschools-location.kml' 
  using driver `KML'
Simple feature collection with 1925 features and 2 fields
Geometry type: POINT
Dimension:     XYZ
Bounding box:  xmin: 103.6824 ymin: 1.247759 xmax: 103.9897 ymax: 1.462134
z_range:       zmin: 0 zmax: 0
Geodetic CRS:  WGS 84
# Retrive geometry list-col
st_geometry(mpsz)
Geometry set for 323 features 
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21
First 5 geometries:
MULTIPOLYGON (((31495.56 30140.01, 31980.96 296...
MULTIPOLYGON (((29092.28 30021.89, 29119.64 300...
MULTIPOLYGON (((29932.33 29879.12, 29947.32 298...
MULTIPOLYGON (((27131.28 30059.73, 27088.33 297...
MULTIPOLYGON (((26451.03 30396.46, 26440.47 303...
# Display associated attribute info
# Reveals data type of each field
glimpse(mpsz)
Rows: 323
Columns: 16
$ OBJECTID   <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
$ SUBZONE_NO <int> 1, 1, 3, 8, 3, 7, 9, 2, 13, 7, 12, 6, 1, 5, 1, 1, 3, 2, 2, …
$ SUBZONE_N  <chr> "MARINA SOUTH", "PEARL'S HILL", "BOAT QUAY", "HENDERSON HIL…
$ SUBZONE_C  <chr> "MSSZ01", "OTSZ01", "SRSZ03", "BMSZ08", "BMSZ03", "BMSZ07",…
$ CA_IND     <chr> "Y", "Y", "Y", "N", "N", "N", "N", "Y", "N", "N", "N", "N",…
$ PLN_AREA_N <chr> "MARINA SOUTH", "OUTRAM", "SINGAPORE RIVER", "BUKIT MERAH",…
$ PLN_AREA_C <chr> "MS", "OT", "SR", "BM", "BM", "BM", "BM", "SR", "QT", "QT",…
$ REGION_N   <chr> "CENTRAL REGION", "CENTRAL REGION", "CENTRAL REGION", "CENT…
$ REGION_C   <chr> "CR", "CR", "CR", "CR", "CR", "CR", "CR", "CR", "CR", "CR",…
$ INC_CRC    <chr> "5ED7EB253F99252E", "8C7149B9EB32EEFC", "C35FEFF02B13E0E5",…
$ FMEL_UPD_D <date> 2014-12-05, 2014-12-05, 2014-12-05, 2014-12-05, 2014-12-05…
$ X_ADDR     <dbl> 31595.84, 28679.06, 29654.96, 26782.83, 26201.96, 25358.82,…
$ Y_ADDR     <dbl> 29220.19, 29782.05, 29974.66, 29933.77, 30005.70, 29991.38,…
$ SHAPE_Leng <dbl> 5267.381, 3506.107, 1740.926, 3313.625, 2825.594, 4428.913,…
$ SHAPE_Area <dbl> 1630379.27, 559816.25, 160807.50, 595428.89, 387429.44, 103…
$ geometry   <MULTIPOLYGON [m]> MULTIPOLYGON (((31495.56 30..., MULTIPOLYGON (…
head(mpsz, n=5)
Simple feature collection with 5 features and 15 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 25867.68 ymin: 28369.47 xmax: 32362.39 ymax: 30435.54
Projected CRS: SVY21
  OBJECTID SUBZONE_NO      SUBZONE_N SUBZONE_C CA_IND      PLN_AREA_N
1        1          1   MARINA SOUTH    MSSZ01      Y    MARINA SOUTH
2        2          1   PEARL'S HILL    OTSZ01      Y          OUTRAM
3        3          3      BOAT QUAY    SRSZ03      Y SINGAPORE RIVER
4        4          8 HENDERSON HILL    BMSZ08      N     BUKIT MERAH
5        5          3        REDHILL    BMSZ03      N     BUKIT MERAH
  PLN_AREA_C       REGION_N REGION_C          INC_CRC FMEL_UPD_D   X_ADDR
1         MS CENTRAL REGION       CR 5ED7EB253F99252E 2014-12-05 31595.84
2         OT CENTRAL REGION       CR 8C7149B9EB32EEFC 2014-12-05 28679.06
3         SR CENTRAL REGION       CR C35FEFF02B13E0E5 2014-12-05 29654.96
4         BM CENTRAL REGION       CR 3775D82C5DDBEFBD 2014-12-05 26782.83
5         BM CENTRAL REGION       CR 85D9ABEF0A40678F 2014-12-05 26201.96
    Y_ADDR SHAPE_Leng SHAPE_Area                       geometry
1 29220.19   5267.381  1630379.3 MULTIPOLYGON (((31495.56 30...
2 29782.05   3506.107   559816.2 MULTIPOLYGON (((29092.28 30...
3 29974.66   1740.926   160807.5 MULTIPOLYGON (((29932.33 29...
4 29933.77   3313.625   595428.9 MULTIPOLYGON (((27131.28 30...
5 30005.70   2825.594   387429.4 MULTIPOLYGON (((26451.03 30...

Plot, will plot up to a reasonable max by default (in this case 9)

plot(mpsz)
Warning: plotting the first 9 out of 15 attributes; use max.plot = 15 to plot
all

Plot out only the geometry

plot(st_geometry(mpsz))

Plot out specific attribute

plot(mpsz["PLN_AREA_N"])

st_crs(mpsz)
Coordinate Reference System:
  User input: SVY21 
  wkt:
PROJCRS["SVY21",
    BASEGEOGCRS["SVY21[WGS84]",
        DATUM["World Geodetic System 1984",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]],
            ID["EPSG",6326]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["Degree",0.0174532925199433]]],
    CONVERSION["unnamed",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]]]
# assign correct EPSG code
mpsz3414 <- st_set_crs(mpsz, 3414)
Warning: st_crs<- : replacing crs does not reproject data; use st_transform for
that
st_geometry(mpsz3414)
Geometry set for 323 features 
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
Projected CRS: SVY21 / Singapore TM
First 5 geometries:
MULTIPOLYGON (((31495.56 30140.01, 31980.96 296...
MULTIPOLYGON (((29092.28 30021.89, 29119.64 300...
MULTIPOLYGON (((29932.33 29879.12, 29947.32 298...
MULTIPOLYGON (((27131.28 30059.73, 27088.33 297...
MULTIPOLYGON (((26451.03 30396.46, 26440.47 303...
preschool3414 <- st_transform(preschool, 
                              crs = 3414)
st_crs(preschool3414)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]
listings <- read_csv("data/aspatial/listings.csv")
Rows: 4161 Columns: 75
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (26): listing_url, source, name, description, neighborhood_overview, pi...
dbl  (37): id, scrape_id, host_id, host_listings_count, host_total_listings_...
lgl   (7): host_is_superhost, host_has_profile_pic, host_identity_verified, ...
date  (5): last_scraped, host_since, calendar_last_scraped, first_review, la...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
list(listings) 
[[1]]
# A tibble: 4,161 × 75
       id listing_url    scrap…¹ last_scr…² source name  descr…³ neigh…⁴ pictu…⁵
    <dbl> <chr>            <dbl> <date>     <chr>  <chr> <chr>   <chr>   <chr>  
 1  50646 https://www.a… 2.02e13 2022-09-23 previ… Plea… Fully … The se… https:…
 2  71609 https://www.a… 2.02e13 2022-09-22 city … Ensu… For 3 … <NA>    https:…
 3  71896 https://www.a… 2.02e13 2022-09-22 city … B&B … <b>The… <NA>    https:…
 4  71903 https://www.a… 2.02e13 2022-09-22 city … Room… Like y… Quiet … https:…
 5 275344 https://www.a… 2.02e13 2022-09-22 city … 15 m… Lovely… Bus st… https:…
 6 289234 https://www.a… 2.02e13 2022-09-22 city … Book… This w… A quie… https:…
 7 294281 https://www.a… 2.02e13 2022-09-22 city … 5 mi… I have… <NA>    https:…
 8 324945 https://www.a… 2.02e13 2022-09-22 city … Cozy… <b>The… <NA>    https:…
 9 330089 https://www.a… 2.02e13 2022-09-22 city … Cozy… A unit… <NA>    https:…
10 330095 https://www.a… 2.02e13 2022-09-22 city … 10 m… Cosy, … Near I… https:…
# … with 4,151 more rows, 66 more variables: host_id <dbl>, host_url <chr>,
#   host_name <chr>, host_since <date>, host_location <chr>, host_about <chr>,
#   host_response_time <chr>, host_response_rate <chr>,
#   host_acceptance_rate <chr>, host_is_superhost <lgl>,
#   host_thumbnail_url <chr>, host_picture_url <chr>, host_neighbourhood <chr>,
#   host_listings_count <dbl>, host_total_listings_count <dbl>,
#   host_verifications <chr>, host_has_profile_pic <lgl>, …
# convert df into simple feature df
# coords arg is c(col name of x coord, col name of y coord)
# crs arg needs coordinates sys in epsg format
# %>%  used to nest st_transform() to transform simple feature data frame into svy21 projected coordinates system
listings_sf <- st_as_sf(listings, 
                       coords = c("longitude", "latitude"),
                       crs=4326) %>%
  st_transform(crs = 3414)
glimpse(listings_sf)
Rows: 4,161
Columns: 74
$ id                                           <dbl> 50646, 71609, 71896, 7190…
$ listing_url                                  <chr> "https://www.airbnb.com/r…
$ scrape_id                                    <dbl> 2.022092e+13, 2.022092e+1…
$ last_scraped                                 <date> 2022-09-23, 2022-09-22, …
$ source                                       <chr> "previous scrape", "city …
$ name                                         <chr> "Pleasant Room along Buki…
$ description                                  <chr> "Fully furnished bedroom …
$ neighborhood_overview                        <chr> "The serenity & quiet sur…
$ picture_url                                  <chr> "https://a0.muscache.com/…
$ host_id                                      <dbl> 227796, 367042, 367042, 3…
$ host_url                                     <chr> "https://www.airbnb.com/u…
$ host_name                                    <chr> "Sujatha", "Belinda", "Be…
$ host_since                                   <date> 2010-09-08, 2011-01-29, …
$ host_location                                <chr> "Singapore", "Singapore",…
$ host_about                                   <chr> "I am a working professio…
$ host_response_time                           <chr> "a few days or more", "wi…
$ host_response_rate                           <chr> "0%", "100%", "100%", "10…
$ host_acceptance_rate                         <chr> "N/A", "100%", "100%", "1…
$ host_is_superhost                            <lgl> FALSE, FALSE, FALSE, FALS…
$ host_thumbnail_url                           <chr> "https://a0.muscache.com/…
$ host_picture_url                             <chr> "https://a0.muscache.com/…
$ host_neighbourhood                           <chr> "Bukit Timah", "Tampines"…
$ host_listings_count                          <dbl> 1, 6, 6, 6, 44, 6, 7, 44,…
$ host_total_listings_count                    <dbl> 4, 15, 15, 15, 57, 15, 8,…
$ host_verifications                           <chr> "['email', 'phone', 'work…
$ host_has_profile_pic                         <lgl> TRUE, TRUE, TRUE, TRUE, T…
$ host_identity_verified                       <lgl> TRUE, TRUE, TRUE, TRUE, T…
$ neighbourhood                                <chr> "Singapore, Singapore", N…
$ neighbourhood_cleansed                       <chr> "Bukit Timah", "Tampines"…
$ neighbourhood_group_cleansed                 <chr> "Central Region", "East R…
$ property_type                                <chr> "Private room in rental u…
$ room_type                                    <chr> "Private room", "Private …
$ accommodates                                 <dbl> 2, 6, 1, 2, 1, 4, 2, 1, 1…
$ bathrooms                                    <lgl> NA, NA, NA, NA, NA, NA, N…
$ bathrooms_text                               <chr> "1 bath", "1 private bath…
$ bedrooms                                     <dbl> 1, 2, 1, 1, 1, 3, 1, 1, N…
$ beds                                         <dbl> 1, 3, 1, 2, 1, 5, 1, 1, 1…
$ amenities                                    <chr> "[\"Gym\", \"Washer\", \"…
$ price                                        <chr> "$80.00", "$145.00", "$85…
$ minimum_nights                               <dbl> 92, 92, 92, 92, 60, 92, 9…
$ maximum_nights                               <dbl> 730, 1125, 1125, 1125, 99…
$ minimum_minimum_nights                       <dbl> 92, 92, 92, 92, 60, 92, 9…
$ maximum_minimum_nights                       <dbl> 92, 92, 92, 92, 60, 92, 9…
$ minimum_maximum_nights                       <dbl> 730, 1125, 1125, 1125, 99…
$ maximum_maximum_nights                       <dbl> 730, 1125, 1125, 1125, 99…
$ minimum_nights_avg_ntm                       <dbl> 92, 92, 92, 92, 60, 92, 9…
$ maximum_nights_avg_ntm                       <dbl> 730, 1125, 1125, 1125, 99…
$ calendar_updated                             <lgl> NA, NA, NA, NA, NA, NA, N…
$ has_availability                             <lgl> TRUE, TRUE, TRUE, TRUE, T…
$ availability_30                              <dbl> 30, 5, 0, 30, 0, 0, 30, 5…
$ availability_60                              <dbl> 60, 35, 1, 60, 0, 0, 60, …
$ availability_90                              <dbl> 90, 65, 1, 90, 21, 10, 90…
$ availability_365                             <dbl> 365, 340, 265, 365, 296, …
$ calendar_last_scraped                        <date> 2022-09-23, 2022-09-22, …
$ number_of_reviews                            <dbl> 18, 20, 24, 47, 14, 12, 1…
$ number_of_reviews_ltm                        <dbl> 0, 0, 0, 0, 1, 0, 0, 3, 2…
$ number_of_reviews_l30d                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ first_review                                 <date> 2014-04-18, 2011-12-19, …
$ last_review                                  <date> 2014-12-26, 2020-01-17, …
$ review_scores_rating                         <dbl> 4.56, 4.44, 4.16, 4.41, 4…
$ review_scores_accuracy                       <dbl> 4.72, 4.37, 4.22, 4.39, 4…
$ review_scores_cleanliness                    <dbl> 4.78, 4.00, 4.09, 4.52, 4…
$ review_scores_checkin                        <dbl> 4.78, 4.63, 4.43, 4.63, 4…
$ review_scores_communication                  <dbl> 4.94, 4.78, 4.43, 4.64, 4…
$ review_scores_location                       <dbl> 4.72, 4.26, 4.17, 4.50, 4…
$ review_scores_value                          <dbl> 4.50, 4.32, 4.04, 4.36, 4…
$ license                                      <chr> NA, NA, NA, NA, "S0399", …
$ instant_bookable                             <lgl> FALSE, FALSE, TRUE, FALSE…
$ calculated_host_listings_count               <dbl> 1, 6, 6, 6, 44, 6, 7, 44,…
$ calculated_host_listings_count_entire_homes  <dbl> 0, 0, 0, 0, 2, 0, 1, 2, 2…
$ calculated_host_listings_count_private_rooms <dbl> 1, 6, 6, 6, 42, 6, 6, 42,…
$ calculated_host_listings_count_shared_rooms  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ reviews_per_month                            <dbl> 0.18, 0.15, 0.18, 0.34, 0…
$ geometry                                     <POINT [m]> POINT (22646.02 351…
# Calculate area of cycling paths
buffer_cycling <- st_buffer(cyclingpath, 
                               dist=5, nQuadSegs = 30)
glimpse(buffer_cycling)
Rows: 2,248
Columns: 3
$ PLANNING_A <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ PLANNING_1 <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ geometry   <POLYGON [m]> POLYGON ((16004.15 36799.78..., POLYGON ((16013.15 …
buffer_cycling$AREA <- st_area(buffer_cycling)
glimpse(buffer_cycling$AREA)
 Units: [m^2] num [1:2248] 186 293 285 145 281 ...
sum(buffer_cycling$AREA)
1556978 [m^2]
# find numbers of pre-schools in each planning subzone
# st_intersects indentified schools located inside planning subzones
# length() calculate num in each zone
mpsz3414$`PreSch Count`<- lengths(st_intersects(mpsz3414, preschool3414))
summary(mpsz3414$`PreSch Count`)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   0.00    0.00    3.00    5.96    9.00   58.00 
# list planning zubzone with most um of pre-school
top_n(mpsz3414, 1, `PreSch Count`)
Simple feature collection with 1 feature and 16 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 39655.33 ymin: 35966 xmax: 42940.57 ymax: 38622.37
Projected CRS: SVY21 / Singapore TM
  OBJECTID SUBZONE_NO     SUBZONE_N SUBZONE_C CA_IND PLN_AREA_N PLN_AREA_C
1      189          2 TAMPINES EAST    TMSZ02      N   TAMPINES         TM
     REGION_N REGION_C          INC_CRC FMEL_UPD_D   X_ADDR   Y_ADDR SHAPE_Leng
1 EAST REGION       ER 21658EAAF84F4D8D 2014-12-05 41122.55 37392.39   10180.62
  SHAPE_Area                       geometry PreSch Count
1    4339824 MULTIPOLYGON (((42196.76 38...           58
glimpse(mpsz3414)
Rows: 323
Columns: 17
$ OBJECTID       <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, …
$ SUBZONE_NO     <int> 1, 1, 3, 8, 3, 7, 9, 2, 13, 7, 12, 6, 1, 5, 1, 1, 3, 2,…
$ SUBZONE_N      <chr> "MARINA SOUTH", "PEARL'S HILL", "BOAT QUAY", "HENDERSON…
$ SUBZONE_C      <chr> "MSSZ01", "OTSZ01", "SRSZ03", "BMSZ08", "BMSZ03", "BMSZ…
$ CA_IND         <chr> "Y", "Y", "Y", "N", "N", "N", "N", "Y", "N", "N", "N", …
$ PLN_AREA_N     <chr> "MARINA SOUTH", "OUTRAM", "SINGAPORE RIVER", "BUKIT MER…
$ PLN_AREA_C     <chr> "MS", "OT", "SR", "BM", "BM", "BM", "BM", "SR", "QT", "…
$ REGION_N       <chr> "CENTRAL REGION", "CENTRAL REGION", "CENTRAL REGION", "…
$ REGION_C       <chr> "CR", "CR", "CR", "CR", "CR", "CR", "CR", "CR", "CR", "…
$ INC_CRC        <chr> "5ED7EB253F99252E", "8C7149B9EB32EEFC", "C35FEFF02B13E0…
$ FMEL_UPD_D     <date> 2014-12-05, 2014-12-05, 2014-12-05, 2014-12-05, 2014-1…
$ X_ADDR         <dbl> 31595.84, 28679.06, 29654.96, 26782.83, 26201.96, 25358…
$ Y_ADDR         <dbl> 29220.19, 29782.05, 29974.66, 29933.77, 30005.70, 29991…
$ SHAPE_Leng     <dbl> 5267.381, 3506.107, 1740.926, 3313.625, 2825.594, 4428.…
$ SHAPE_Area     <dbl> 1630379.27, 559816.25, 160807.50, 595428.89, 387429.44,…
$ geometry       <MULTIPOLYGON [m]> MULTIPOLYGON (((31495.56 30..., MULTIPOLYG…
$ `PreSch Count` <int> 0, 5, 0, 4, 2, 10, 4, 2, 6, 1, 6, 2, 0, 1, 6, 0, 0, 0, …
# Calc density of pre-school by planning subzone
mpsz3414$Area <- mpsz3414 %>%
  st_area()
mpsz3414 <- mpsz3414 %>%
  mutate(`PreSch Density` = `PreSch Count`/Area * 1000000)
head(mpsz3414)
Simple feature collection with 6 features and 18 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 24468.89 ymin: 28369.47 xmax: 32362.39 ymax: 30542.74
Projected CRS: SVY21 / Singapore TM
  OBJECTID SUBZONE_NO      SUBZONE_N SUBZONE_C CA_IND      PLN_AREA_N
1        1          1   MARINA SOUTH    MSSZ01      Y    MARINA SOUTH
2        2          1   PEARL'S HILL    OTSZ01      Y          OUTRAM
3        3          3      BOAT QUAY    SRSZ03      Y SINGAPORE RIVER
4        4          8 HENDERSON HILL    BMSZ08      N     BUKIT MERAH
5        5          3        REDHILL    BMSZ03      N     BUKIT MERAH
6        6          7 ALEXANDRA HILL    BMSZ07      N     BUKIT MERAH
  PLN_AREA_C       REGION_N REGION_C          INC_CRC FMEL_UPD_D   X_ADDR
1         MS CENTRAL REGION       CR 5ED7EB253F99252E 2014-12-05 31595.84
2         OT CENTRAL REGION       CR 8C7149B9EB32EEFC 2014-12-05 28679.06
3         SR CENTRAL REGION       CR C35FEFF02B13E0E5 2014-12-05 29654.96
4         BM CENTRAL REGION       CR 3775D82C5DDBEFBD 2014-12-05 26782.83
5         BM CENTRAL REGION       CR 85D9ABEF0A40678F 2014-12-05 26201.96
6         BM CENTRAL REGION       CR 9D286521EF5E3B59 2014-12-05 25358.82
    Y_ADDR SHAPE_Leng SHAPE_Area                       geometry PreSch Count
1 29220.19   5267.381  1630379.3 MULTIPOLYGON (((31495.56 30...            0
2 29782.05   3506.107   559816.2 MULTIPOLYGON (((29092.28 30...            5
3 29974.66   1740.926   160807.5 MULTIPOLYGON (((29932.33 29...            0
4 29933.77   3313.625   595428.9 MULTIPOLYGON (((27131.28 30...            4
5 30005.70   2825.594   387429.4 MULTIPOLYGON (((26451.03 30...            2
6 29991.38   4428.913  1030378.8 MULTIPOLYGON (((25899.7 297...           10
             Area   PreSch Density
1 1630379.3 [m^2] 0.000000 [1/m^2]
2  559816.2 [m^2] 8.931502 [1/m^2]
3  160807.5 [m^2] 0.000000 [1/m^2]
4  595428.9 [m^2] 6.717847 [1/m^2]
5  387429.4 [m^2] 5.162230 [1/m^2]
6 1030378.8 [m^2] 9.705169 [1/m^2]
hist(mpsz3414$`PreSch Density`)

ggplot(data=mpsz3414, 
       aes(x= as.numeric(`PreSch Density`)))+
  geom_histogram(bins=20, 
                 color="black", 
                 fill="light blue") +
  labs(title = "Are pre-school even distributed in Singapore?",
       subtitle= "There are many planning sub-zones with a single pre-school, on the other hand, \nthere are two planning sub-zones with at least 20 pre-schools",
      x = "Pre-school density (per km sq)",
      y = "Frequency")

ggplot(data=mpsz3414, 
       aes(y = `PreSch Count`, 
           x= as.numeric(`PreSch Density`)))+
  geom_point(color="black", 
             fill="light blue") +
  xlim(0, 40) +
  ylim(0, 40) +
  labs(title = "",
      x = "Pre-school density (per km sq)",
      y = "Pre-school count")
Warning: Removed 2 rows containing missing values (`geom_point()`).