Is there a way to replicate the Split Vector Layer function from QGIS in R?

I have a Shapefile containing around 20 polygons that I'd like to split into 20 separate files.

I guess since it's in QGIS there would be a solution in Python, but I'd like to integrate it into a R workflow if possible.

1 Answer 1


Yes, you can just loop through the individual features and save them like so:

library (sf)

dat <- read_sf ("~/data.shp")

for (i in seq_len (nrow (dat)))
  fname <- paste0 ("~/split_", i, ".shp")
  write_sf (dat [i, ], fname)
  • The use of ~ always gets me. In 90% of R it's used purely for modeling, and it takes a hot minute to remember that it means 'home directory'.
    – Mox
    Jan 19, 2020 at 21:54

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.