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 do this with  two steps. First,  I have 
created  100 csv files. But,  I when I am trying to merge these  files 
with a shape file (Step 2),  it show error massage.  It create only 
three shape files (sim1, sim2, sim3). I have not able figured out  the 
source of error in code. Any suggestion?  Thanks

Error in writeOGR(thana, dsn = "/GWPR/Shape_Files", paste("sim", i, 
".shp",  :
   Non-unique field names

#-------------------
library(spdep)
library(gstat)
library(maptools)
library(maps)
library(rgdal)
library(foreign)
######
setwd("H:/GWPR/csv_files")

id <- read.csv("id_file.csv", as.is = TRUE)
gas <- read.csv('thana_sis_logGAS_intake_246.csv', as.is = TRUE)
total <- read.csv("thana_sis_logTotal_intake_246.csv", as.is = TRUE)
was <- read.csv("thana_sis_logWAS_intake_246.csv", as.is = TRUE)

thana <- readOGR(".", "bd_thana_246") #
OGR data source with driver: ESRI Shapefile
Source: ".", layer: "bd_thana_246"
with 246 features and 1 fields
Feature type: wkbPolygon with 2 dimensions
 > str(thana)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
   ..@ data       :'data.frame': 246 obs. of  1 variable:
   .. ..$ THANA_ID: num [1:246] 4 20 31 37 171 46 282 305 463 67 ...
   ..@ polygons   :List of 246


  # Step: 1: CSV files

for (i in 1:100){
key <- paste('sim', i, sep = "")
write.csv(data.frame(THANA_ID = id$THANA_ID
, PatNum = id$PatNum
, LogWAS = was[[key]]
, LogGAS = gas[[key]]
, LogTotal = total[[key]]
, stringsAsFactors = FALSE
  )
  , paste(key, '.csv', sep = '')
   )}

  # Step: 2: Merge with

for (i in 1:100){
     x <- read.csv(paste('sim', i, '.csv', sep = ''), as.is = TRUE)
     thana@data <- merge(thana@data, x, by.x="THANA_ID",by.y="THANA_ID", 
all.x = TRUE,
sort = FALSE)
     writeOGR(thana, dsn = "/GWPR/Shape_Files", paste('sim',  i,".shp", 
sep =
''), 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 zua3@cornell.edu



	[[alternative HTML version deleted]]

