# install the necessary packages 
install.packages("sf")
install.packages("readxl")
install.packages("dplyr")


### **Loading Libraries**
load the required libraries

```r
# Load necessary libraries
library(sf)        # For shape files
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(readxl)    # For reading Excel files
library(dplyr)     # For data manipulation (merging)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(knitr)

# Import eastern North Carolina (ENC) shapefile
ENC <- "~/Phd Research Work/Build permits/NC Census Tract" 
ENC_Census_Tract <- st_read(ENC)
## Multiple layers are present in data source C:\Users\ADENIJIN21\OneDrive - East Carolina University\Documents\Phd Research Work\Build permits\NC Census Tract, reading layer `44 Counties'.
## Use `st_layers' to list all layer names and their type in a data source.
## Set the `layer' argument in `st_read' to read a particular layer.
## Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
## automatically selected the first layer in a data source containing more than
## one.
## Reading layer `44 Counties' from data source 
##   `C:\Users\ADENIJIN21\OneDrive - East Carolina University\Documents\Phd Research Work\Build permits\NC Census Tract' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 44 features and 9 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -79.69295 ymin: 33.84232 xmax: -75.46062 ymax: 36.55209
## Geodetic CRS:  NAD83
kable(head(ENC_Census_Tract))
STATEFP COUNTYFP COUNTYNS AFFGEOID GEOID NAME LSAD ALAND AWATER geometry
37 049 01026123 0500000US37049 37049 Craven 06 1830077186 172715012 MULTIPOLYGON (((-77.47329 3…
37 055 01026133 0500000US37055 37055 Dare 06 992549370 3000522458 MULTIPOLYGON (((-75.72681 3…
37 069 01008553 0500000US37069 37069 Franklin 06 1273757057 7178682 MULTIPOLYGON (((-78.54551 3…
37 083 01008559 0500000US37083 37083 Halifax 06 1874467579 16925017 MULTIPOLYGON (((-78.00655 3…
37 091 01026127 0500000US37091 37091 Hertford 06 914689326 18737419 MULTIPOLYGON (((-77.20861 3…
37 093 01008563 0500000US37093 37093 Hoke 06 1010400416 3957346 MULTIPOLYGON (((-79.45792 3…
plot(st_geometry(ENC_Census_Tract), main = "Map of ENC")

# Import Excel file
excel_path <- "~/Phd Research Work/Build permits/Loss model/Loss_Tract_H.xlsx"
excel_data <- read_excel(excel_path, sheet = 1)  # Use `sheet` to specify which sheet if needed
kable(head(excel_data))
TRACT H1 H2 H3 H4 H5 H6 H7 H8 H9 H10 H11 H12 H13 H14 H15
37001020100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
37001020200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
37001020300 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
37001020400 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
37001020501 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
37001020502 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0