Home > Enterprise >  Calculating area of wetland types within a buffer
Calculating area of wetland types within a buffer

Time:07-29

I am fairly new to R, and I'm running into errors when I try to calculate the area of each wetland type within my buffers. When I attempt to group the data and summarize the area by wetland type, I receive the error UseMethod () no applicable method for 'group_by' applied to an object of class c(SpatialPolygonsDataFrame). How can I fix this?

library(tidyverse)
library(rgeos)
library(tidyr)
library(sf)

Neosho_wetlands<- readOGR("HU8_11070205_watershed", layer="HU8_11070205_Wetlands")
Spring_wetlands<- readOGR("HU8_11070207_watershed", layer="HU8_11070207_Wetlands")
North_wetlands<- readOGR("HU8_10290104_watershed", layer="HU8_10290104_Wetlands")
South_wetlands<- readOGR("HU8_11070206_watershed", layer="HU8_11070206_Wetlands")

wetlands<-rbind(Neosho_wetlands, Spring_wetlands, North_wetlands, South_wetlands)
rm("Neosho_wetlands", "Spring_wetlands","North_wetlands","South_wetlands")
plot(wetlands)

points<-read.csv("SiteCov.csv")
coordinates(points)<-points[c("long", "lat")]
proj4string(points)<-CRS(" proj=longlat")
points_sp<-spTransform(points, crs(wetlands))

buffer2<-buffer(points_sp[1:32,], 2000)
crop2<-crop(wetlands, buffer2)
plot(crop2)
as.data.frame(crop2)

crop2 %>% group_by(WETLAND_TY) %>%
  summarize(sum("Freshwater Emergent Wetland",
                "Freshwater Forested/Shrub Wetland",
                "Freshwater Pond",
                "Lake",
                "Riverine"))

CodePudding user response:

You currently run as.data.frame(crop2) but do not assign it to an object. Accordingly, crop2 is still a SpatialPolygonsDataFrame when you pass it to group_by() and it doesn't play nicely with it. You should be able to simply change as.data.frame(crop2) to crop2 <- as.data.frame(crop2) and be good. Otherwise, you can do as Jonathan noted in the comments and use st_read().

  • Related