if (!requireNamespace(“sf”, quietly = TRUE)) install.packages(“sf”) if (!requireNamespace(“terra”, quietly = TRUE)) install.packages(“terra”) if (!requireNamespace(“dplyr”, quietly = TRUE)) install.packages(“dplyr”)
library(sf) library(terra) library(dplyr)
b5 <- rast(“./rs/LC08_044034_20170614_B5.tif”) b4 <- rast(“./rs/LC08_044034_20170614_B4.tif”)
ndvi <- (b5 - b4) / (b5 + b4)
ca_places <- st_read(“./ca_places/ca_places.shp”)
#Reading layer
ca_places' from data source/cloud/project/ca_places/ca_places.shp’
using driver `ESRI Shapefile’ #Simple feature collection with 1618
features and 16 fields #Geometry type: MULTIPOLYGON #Dimension: XY
#Bounding box: xmin: -124.2695 ymin: 32.53432 xmax: -114.229 ymax:
41.99317 #Geodetic CRS: WGS 84
bethel <- ca_places %>% filter(NAME == “Bethel Island”) oakley <- ca_places %>% filter(NAME == “Oakley”)
bethel_ndvi <- extract(ndvi, bethel, fun = median) bethel_median_ndvi <- bethel_ndvi[, 2]
oakley_ndvi <- extract(ndvi, oakley, fun = median) oakley_median_ndvi <- oakley_ndvi[, 2]
cat(“Median NDVI for Bethel Island:”, bethel_median_ndvi, “”) cat(“Median NDVI for Oakley:”, oakley_median_ndvi, “”)
#Median NDVI for Bethel Island: 0.4180438 #Median NDVI for Oakley: 0.3005387
bethel_values <- extract(ndvi, bethel)[, 2] oakley_values <- extract(ndvi, oakley)[, 2]
t_test_result <- t.test(bethel_values, oakley_values) print(t_test_result)
##Welch Two Sample t-test #data: bethel_values and oakley_values #t = 45.979, df = 26353, p-value < 2.2e-16 #alternative hypothesis: true difference in means is not equal to 0 #95 percent confidence interval: # 0.07298078 0.07948010 #sample estimates: #mean of x mean of y #0.4124472 0.3362167