[R-sig-Geo] Importing multiple shapefiles to run a loop in R

Edzer Pebesma edzer.pebesma at uni-muenster.de
Mon Mar 14 20:11:31 CET 2011


Nathaniel, unlike a .csv, a shapefile consists of a set of files. The
functions you use to read them take either the base name ("shape1") or
the .shp file ("shape1.shp") as argument, and discover the rest
themselves. They don't like to be called with e.g. "shape1.dbf".

If your understanding of R is rudimentary, try first to read single
shapefiles in R, then try to understand how lapply works.

On 03/14/2011 08:04 PM, Nathaniel Rayl wrote:
> 
> 
> 
> 
> 
> 
> 
> Hi R Users,
> 
> I commonly import multiple .csv files and then write loops to work with those files like this:
> 
>> setwd('C://Nathaniel/R/allfiles')
>> files<-list.files()
>> allfiles<-lapply(t1,read.csv)
>> for (i in 1:47) {
>>     t1<-allfiles[[i]]
> 
> etc. etc.
> 
> I have written a script that I would like to loop to work with a folder of 400+ .shp files.  Is there an equivalent way to read in all the shapefiles so that I can run a loop?
> 
> I tried this:
> 
>> setwd('C://Nathaniel/R/shapefiles')
>> t1<-list.files()
>> t2<-lapply(t1,readShapePoly)
> 
> which returned this error:
> 
> "Error in Fun(c("shape1.dbf", "shape1.prj", "shape1.sbn", "shape1.sbx", :
>    missing layer"
> 
> and I tried this:
> 
>> setwd('C://Nathaniel/R/shapefiles')
> 
>> t1<-list.files()
> 
>> t2<-lapply(t1,readOGR)
> 
> which returned this error:
> 
> "Error in getinfo.shape(filen) : Error opening SHP file"
> 
> 
> I've searched the archives and wasn't able to find anything like this.  My understanding of R code is rudimentary, so any help would be greatly appreciated.  Thanks for considering my problem.
> 
> Best regards,
> 
> Nathaniel
>  		 	   		  
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list