Use the shapefile directly. You can do this easily with the
sf packages, and read the shape in an object. For both packages you need to provide
dsn - the data source, which in the case of a shapefile is the directory, and
layer - which is the shapefile name, minus extension:
# Read SHAPEFILE.shp from the current working directory (".")
shape <- readOGR(dsn = ".", layer = "SHAPEFILE")
shape <- read_sf(dsn = ".", layer = "SHAPEFILE")
(For rgdal, in OSX or Linux you can't use the '~' shorthand for the home directory as the data source (
dsn) directory - otherwise you'll get an unhelpful "Cannot open data source" message. The
sf package doesn't have this limitation, among some other advantages.)
This will give you an object which is a Spatial*DataFrame (points, lines or polygons) - the fields of the attribute table are then accessible to you in the same way as an ordinary dataframe, i.e.
shape$ID for the ID column.
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.:
shape$x <- as.numeric(as.character(shape$x))
shape$y <- as.numeric(as.character(shape$y))
coordinates(shape) <- ~x + y
Edit 2015-01-18: note that rgdal is a bit better than maptools (which I initially suggested here), primarily because it reads and writes projection information automatically.
- the nested
as.numeric(as.character()) functions - if your ASCII text was read as a factor (likely), this ensures that you get the numeric values instead of the factor levels.
sf have confusing ways of accessing different file and database types (e.g. for a GPX file, the dsn is the filename, and layers the individual components such as waypoints, trackpoints, etc), and careful reading of online examples is needed.