Skip to content
Prev 2705 / 29559 Next

Subsetting SpatialPolygonsDataFrame and empty ID slots

That's really helpful, thanks.

For "everyday" use, I've made:

"slotspol" <- function(pol, sl="ID")
sapply(slot(pol, "polygons"), function(x) slot(x, sl))

Which can be used for any slot name:
slotspol(Up)
slotspol(Up,"area")

Also, few questions:
1. I observe that pols$Site is identical to pols at data$Site. Is not 
pols at data$Site the correct syntax? I mean that we retrieve variable 
"Site" from slot "data" in object "pols".

2. In your code, I don't understand
sites_Lupuna <- length(grep("Lupuna", as.character(pols$Site))) > 0

because the result is just T (the result of length(grep("Lupuna", 
as.character(pols$Site)))  is 3)
and thus subsetting with sites_Lupuna yields an object that is identical
to the original:

 > pols_Lupuna <- pols[sites_Lupuna,]
 > length(pols_Lupuna at polygons)
[1] 140
 > length(pols at polygons)
[1] 140

Instead:
 > sites_Lupuna <- grep("Lupuna", as.character(pols$Site))
 > sites_Lupuna
[1] 1 2 3
 > pols_Lupuna <- pols[sites_Lupuna,]

seems to provide a correctly subset of the original pols object ("Lupuna 
1", "Lupuna 2", "Lupuna 3")

Am I wrong? I feel quite insecure on this.

Agus

Roger Bivand escribi?: