Attributes with dplyr

library(sf)
library(dplyr)

# Create path to gdb
gdbPath <- "data/source/segi/2023_GS_Groves_OTI_public.gdb"

segi <- st_read(gdbPath, layer = "SEGI_OTI_2023_public")

# Filter rows and select columns
filter(segi, Lidar_Canopy_Ht > 80)
select(segi, Grove)

ch <- filter(segi, Lidar_Canopy_Ht > 80)
ch <- select(ch, Grove, Status, Lidar_Canopy_Ht)
# Use piping (chaining together functions) to accomplish the same task
segi_subset <- segi %>% 
  filter(Lidar_Canopy_Ht > 80) %>% 
  select(Grove, Status, Lidar_Canopy_Ht)

# Summarize data by group variable
# Mean and max canopy height per grove
# Drop the geometry if you just want a data.frame
segi_sum <- segi %>% 
  filter(!is.na(Lidar_Canopy_Ht)) %>% 
  group_by(Grove) %>% 
  summarise(mean_canopy_ht = mean(Lidar_Canopy_Ht), max_canopy_ht = max(Lidar_Canopy_Ht)) %>% 
  st_drop_geometry()
# filter the result to see the grove containing the record for max canopy height
filter(segi_sum, max_canopy_ht == max(max_canopy_ht))