I need to open a shape file from ArcMap in R to use it for further geostatical analysis. I've converted it into ASCII text file, but in R it is recognized as data.frame. Coordinates function doesn't work as soon as x and y are recognized as non-numeric. Could you help to deal with it?
|
|
I agree with gissolved that you should use the shapefile directly ... the easiest way to do this is to ensure you have the maptools package installed, and read the shape as an object:
This will give you an object which is a SpatialPointsDataFrame - the fields of the attribute table are then accessible to you in the same way as an ordinary dataframe, i.e. If you want to use the ASCII file you imported, then you should simply convert the text (character) x and y fields to numbers, e.g.:
|
|||
|
|
|
I think you shouldn't convert the shapefile to an ASCII but instead use the shapefile directly with one of the spatial extensions. Here you can find a three ways to read (and write) a shapefile http://www.nceas.ucsb.edu/scicomp/usecases/ReadWriteESRIShapeFiles. The R-spatial project will probably also interest you http://cran.r-project.org/web/packages/sp/index.html. |
|||
|
|
|
I agree with the Simbamangu and gissolved in terms of retaining the shapefile but want to direct your attention specifically to the rgdal library. Follow the link suggested by gissolved for the NCEAS and follow through with the directions for rgdal. It can be challenging to install on some machines but it can substantially improve results when it comes to projections. The maptools library is excellent and allows you to define the projection for the shapefile you are reading in, but to do so you need to know how to specify that projection in the proj4 format. an example might look something like:
If you want to go this route, then I recommend http://spatialreference.org as the place to go to figure out what your projection looks like in the proj4 format. If that looks like a hassle to you, rgdal will make it easy by reading the ESRI shapefile's .prj file (the file that contains ESRI's projection definition for the shapefile. To use rgdal on the same file you would simply write:
You can likely skate by without doing this if you are just working with a single shapefile, but as soon as you start looking at multiple data sources or overlaying with Google Maps, keeping your projections in good shape becomes essential. For some helpful walkthroughs on spatial data in R, including a bunch of stuff on importing and working with point patterns, I have some old course materials online at http://csde.washington.edu/services/gis/workshops/SPATIALR.shtml that might help you see how these methods compare in practice. |
|||
|
|
One more alternative is to use fastshp library which offers::
Here is my question on SE on how to use it with ggplot2: |
|||
|
