I'm working with shapefile in R and I need to calculate the polygons' area that I created by Gdiference function, So ,now I need to create a column with the size of freshly polygons created, because after that I need take out the small polygons which are not important to my academic research

I didn't use any function yet.

  • Take a look at ?rgeos::gArea Jun 27 '18 at 1:20

Can you provide data for an example? Anyways, try this:

f <- st_read("shapefile.shp")
f$area <- st_area(f) #Take care of units


  • It worked, but when I tried to use the shapefile function, returns me a error. Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘shapefile’ for signature ‘"sf"’ Jun 27 '18 at 1:43
  • 1
    The raster::shapefile function is for reading data from on-disk files and returns a polygon object using the sp package's methods for representing spatial data in R (in this case, a SpatialPolygonsDataFrame). If you want to convert the sf polygon dataframe back to an SPDF, use as(f, 'Spatial'). To go from sp to sf, you can use sf::st_as_sf(f). If all you want to do is write your result back to disk, you don't have to convert to sp first - just use sf::st_write().
    – obrl_soil
    Jun 27 '18 at 3:24

Get area using dplyr::mutate and sf::st_area

library(sf); library(dplyr)
p <- st_read("polygon.shp")

p %>% 
  mutate(area = st_area(p))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.