Import multiple shapefiles and export a .csv file

Hello there,
I am quite new to R studio, and I'm trying to read multiple shapefiles (with random names) from a single directory and extract the number of row s of data with each shapefile's table of attribute and finally export data in a .csv file comprising the shapefiles name in the first column and number of rows of the table of attribute corresponding to each shapefile in the second column.
I appreciate any tip help me to accomplish this task.

Hi, and welcome to this nice community! What's a shapefile? Could you share a reprex (reproducible example) following the approach outlined here? Thanks!!

1 Like

Shapefiles can mean many things - are they ESRI shapefiles, or other format? Are they individual shp files, or a gdb database? What package are you using to access them? The "old" rgdal & sp workflow, or the "new" sf workflow? Hard to give definite answer without knowing this first.

The start of my approach would be this:

library(rdgal)

shapefiles <- list.files(pattern= '*.shp')

result <- data.frame(names = shapefiles,
                     rows = c(NA)) # initialize the count

for (i in seq_along(shapefiles)) {
  shp <- readOGR(dsn = ".", layer = tools::file_path_sans_ext(shapefiles[i])) 
    # dot as dsn = assumes current direcotry; layer = filename without extension
  result$rows[i] <- nrow(shp@data) 
    # rows in the data slot of shp as s3 object
}

write.csv(result, "result.csv")
4 Likes

Thanks a lot for provided response.
They are individual .shp files and I'm using old rgdal to access them.

I have slightly amended my code above to follow the rgdal workflow. I don't have a directory full of shapefiles on hand, so I can't test it, but it should work; and even if it does not the gist is there.

You might wish to consider switching to the excellent sf package, which takes a different approach to handling spatial and data elements than sp package (which is what rgdal uses).

  • In sp world the primary role is played by geometry as items in a S3 object, the last slot of which is special "data" slot containing data.frame.

  • In sf world the primary role is played by data as (modified) data.frame, the last column of which is "geometry" containing geometry.

The difference is that while sf objects are capable of pretty much the same things as sp objects on the spatial front they are much, much easier to use on the data front. Since they are modified data.frames all the usual data.frame tools apply (dplyr, I am looking at you!)

3 Likes