Hi Zia, There are two methods were you can work: the for statement and the tapply function.
First, take a good look on these: ?"for" and ?tapply You can try something like this: for (i in 1:100) sim<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$[,i], logGAs=gas$[,i],logTotal=total$[,i]) sim.shp <- thana sim.shp@data <- merge(thana@data,sim,by.x="THANA_ID",by.y="THANA_ID", all.x=T, sort=F) writeOGR(sim.shp,dsn="/GWPR/Shape_Files", layer=paste("sim_",i,".shp",sep=""),driver="ESRI Shapefile") A better way is use create a list of variables, so i will assume the name of the variable. On Mon, Mar 12, 2012 at 10:53 AM, Zia Ahmed <z...@cornell.edu> wrote: > I like to write a r-function to create 100 shape files in a loop. I > have three data files. Each file has 100 data fields (like sim1, sim2, > sim3....sim100) with a unique ID fields (Thana_ID). First I want to > create 100 data frame from these files and then merge or join each of > this 100 files with a polygon files individually to create 100 ESRI > shape files. I am trying to this following way. But I need do this in a > loop. Any suggestion will be appreciated. > > Thanks > Zia > > #-------------------------------------------------- > > library(sp) > library(spdep) > library(gstat) > library(maptools) > library(maps) > library(rgdal) > > ###### > setwd("H:/GWPR") > > gas<-read.csv("thana_sis_logGAS_intake_246.csv",header=TRUE) > was<-read.csv("thana_sis_logWAS_intake_246.csv",header=TRUE) > total<-read.csv("thana_sis_logTotal_intake_246.csv",header=TRUE) > names(thana) > names(was) > names(gas) > names(total > > # Create 100 data-frames by selecting one column from each file. > > sim1<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim1, logGAs=gas$sim1, > logTotal=total$sim1) > > sim2<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim2, logGAs=gas$sim2, > logTotal=total$sim2) > . > . > . > sim100<-cbind(THANA_ID=thana$THANA_ID, logWAS=was$sim100, > logGAs=gas$sim100, logTotal=total$sim100) > > > > #Join each data framewith a shape file and then write #ESRIShape files. > # Total number of shapefiles willbe100. > > sim1.thana<-readShapePoly("bd_thana_246.shp") > > sim1.thana@data <- merge(thana@data,sim1,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim1.thana,dsn="/GWPR/Shape_Files", layer="sim1.thana", > driver="ESRI Shapefile") > > sim2.thana<-readShapePoly("bd_thana_246.shp") > > sim2.thana@data <- merge(thana@data,sim2,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim2.thana,dsn="/GWPR/Shape_Files", layer="sim2.thana", > driver="ESRI Shapefile") > > . > > . > > . > > sim100.thana<-readShapePoly("bd_thana_246.shp") > > sim100.thana@data <- merge(thana@data,sim100,by.x="THANA_ID", > by.y="THANA_ID", all.x=T, sort=F) > > writeOGR(sim100.thana,dsn="/GWPR/Shape_Files", layer="sim100.thana", > driver="ESRI Shapefile") > > > > -- > --------------------- > Zia Ahmed, PhD > Research Associate > Department of Crop and Soil Sciences > 1002 Bradfield Hall, Cornell University > Ithaca, NY 14853-4203 > t. 607.255.9387 > f. 607.255.3207 > email z...@cornell.edu > > > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > -- Atenciosamente, Raphael Saldanha saldanha.plan...@gmail.com [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo