Skip to content
Prev 4263 / 29559 Next

Modifying a column in a Shapefile

On Fri, 3 Oct 2008, Juliane Struve wrote:

            
Difficult to know where to start. If you had used a provided data set, it 
might have been easier.

First, to coerce to a data.frame, use as(), the coerce method:

MC <- as(MergedCentroids, "data.frame")

What actually got written to the CSV file probably resulted from coercion 
being invoked internally in write.table() - did you inspect the file?

Why not just operate on the MergedCentroids object?

MergedCentroids$NEARANG_R <- ifelse(MergedCentroids$NEARANGLE<0,
  MergedCentroids$NEARANGLE+360, MergedCentroids$NEARANG)

is quite OK. Why not then simply use:

writeOGR(MergedCentroids, "NEWMCSPDF", driver="ESRI Shapefile")

I can imagine that the input object is polygons not points, adding one 
possible extra level, and that the OGR driver does not handle dates 
gracefully, leading you to prefer writeSpatialShape() in maptools (using 
the improved write.dbf() in foreign), but not much more reason for the 
complications you introduce. Simplifying usually helps, as does giving 
working examples from the included data sets.

Roger