I would like to carry out a subsetting in my shapefile without specifying the name of the first column in the .dbf file. To be more precise I would like to select all the rows with value 1 in the first column of the .dbf, but I don't want to specify the name of this column.
For example this script works because I specify the name of the column (as columnName)
library(rgdal) # readOGR
shapeIn <- readOGR(nomeFile)
shapeOut <- subset(shapeIn, columnName == 1)
instead it doesn't works
shapeOut <- (shapeIn[,1] == 1)
and I get an error message: comparison (1) is possible only for atomic and list types shapeOut and shapeIn are ESRI vector files.
This is the header of my shapeIn
coordinates mask_1000_
1 (54000, 1218000) 0
2 (55000, 1218000) 0
3 (56000, 1218000) 0
Can you help me? Thank you
This
shapeOut <- (shapeIn[,1] == 1)
doesn't work beacuse SpatialPolygonsDataFrames contain other info other than the data. So "common" data.frame subsetting doesn't work in the same way. To have it work, you must make the "logical check" for subsetting on the @data slot: this should work (either using subset or "direct" indexing):
shapeOut <- subset(shapeIn, shapeIn@data[,1] == 1)
OR
shapeOut <- shapeIn[shapeIn@data[,1] == 1,]
(however, by recent experience, referencing to data by column number is seldom a good idea... ;-) )
ciao Giacomo !!!