5

The data set of shapefile "Property1A" is:

 df
#      suburb        area asst
# 0     Te Aro   14.541780 R076
# 1     Te Aro    7.655428 R076
# 2     Te Aro 3189.976134 <NA>
# 3     Te Aro  242.173386 0132
# 4     Karori  537.697290 R003
# 5    Pipitea 1116.954993 R105
# 6    Mt Cook  106.099900 R151
# 7     Te Aro   83.307421 R057
# 8 Aro Valley  544.665788 R105
# 9 Aro Valley   59.726882 R110

When I try to subset the shapefile for suburb "Te Aro", the code I use is:

TeAro <- subset(Property1A, suburb == "Te Aro") 

But there is an error:

TeAro <- subset(Property1A, suburb == "Te Aro")
# Error in eval(expr, envir, enclos) : object 'suburb' not found

Why there is an error?

mnel
  • 113,303
  • 27
  • 265
  • 254
Bingbing Liu
  • 61
  • 1
  • 1
  • 3
  • A reproducible example please. Did you create an object called `Property1A` (your example suggests you have an object called `df`)? How did you read this shape file in? – mnel Sep 23 '14 at 01:19
  • df <- Property1A@data – Bingbing Liu Sep 23 '14 at 01:24
  • Property1A is a shapefile – Bingbing Liu Sep 23 '14 at 01:24
  • By the way, how to edit the format of my question. Thanks. – Bingbing Liu Sep 23 '14 at 01:25
  • 1
    What do you mean "Property1A" is a shapefile. How did you read it into `R`? – mnel Sep 23 '14 at 01:33
  • library(rgdal) library(shapefiles) – Bingbing Liu Sep 23 '14 at 01:40
  • # library(rgdal) # library(shapefiles) # Property1A <- readOGR(".", "Properties_1A") There are four files making up a shapefile, a dbf file, a sbn file, a shp file and a shx file. I read the shapefile into R and use ShapeFileName@data to get the data containing in the shapefile. I check the data in the shapefile and can affirm that there is variable "suburb" including in the data, but I feel confused why I cannot subset the shapefile by "suburb". – Bingbing Liu Sep 23 '14 at 01:45
  • 2
    `Property1[Property1$suburb %in% "Te Aro",]` –  Sep 23 '14 at 01:55
  • Ok, got it. Perhaps it's a polygon shapefile, while I try TeAro <- subset(Property1A, Property1A$suburb == "Te Aro"). It works. – Bingbing Liu Sep 23 '14 at 01:56
  • @Pascal: Thanks. I feel quite confused. Before I ever tried to subset the line shapefile, and the code above works. Why there is an error this time? Because the shapefile is SpatialPolygons? – Bingbing Liu Sep 23 '14 at 02:08
  • Its **NOT** a shapefile. Its something read in from a shapefile. Its a `SpatialPolygonsDataFrame` - you can create these from all sorts of spatial data objects and databases, not just shapefiles. – Spacedman Sep 23 '14 at 07:29

1 Answers1

11

subset with names should work. You don't need the shapefiles package if you are reading in with readOGR:

> require(rgdal)
Loading required package: rgdal
Loading required package: sp
rgdal: version: 0.8-14, (SVN revision 496)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.9.0, released 2011/12/29
Path to GDAL shared files: /usr/share/gdal/1.9
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 470]
Path to PROJ.4 shared files: (autodetected)

Get the 169 polygons of Indian states into a spatial object:

> India = readOGR(".","india_state")
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "india_state"
with 169 features and 3 fields
Feature type: wkbPolygon with 2 dimensions

Now subset by name:

> Gujarat = subset(India, NAME=="Gujarat")
> dim(Gujarat)
[1] 12  3
> dim(India)
[1] 169   3

I can plot these objects, they map nicely:

> plot(Gujarat)

Without your data or knowing what version of R and the packages you have, there is no way of knowing why this fails for you.

Package: rgdal Version: 0.8-14

Package: sp Version: 1.0-14

R version 3.0.2....

Spacedman
  • 92,590
  • 12
  • 140
  • 224