I have a shapefile witch contains several empty polygons. I´m extracting values from raster file based in some polygons of this shapefile. But my loop stops at every empty polygon.
Simple solution would be to remove NAs with na.omit(shape@data)
but this deletes some xy values as well.
Here is my code. The loop stops once it finds a empty polygon.
all.mun <- NULL
for (j in mun.codes)
{
mun <- mun.br[mun.br$CODIGO_MUN == j, ] # isolate municipality
suit.mun <- extract(suit, mun) # extract suits from the municipality
suit.mun <- do.call("c", suit.mun) # convert to data frame no matter how many lists the function extract::raster returns
suit.mean <- mean(suit.mun) # mean suitabilitie of the site
all.mun <- rbind(all.mun, suit.mean) # saving result by observation, i.e municipalitie
}
mun.condes
is a vector with a list of 6 digit municipalities codes.
mun.br
is the shapefile of the municipalities
> names(mun.br)
[1] "ID" "NOMEUF" "REGIAO" "CODMESO" "NOMEMESO" "CODMICRO"
[7] "NOMEMICRO" "NOME_1" "NOME_2" "CODIGO_UF" "CODIGO_MUN" "AREA_1997"
Here is the error message.
Error in y[i, ] :
cannot get a slot ("Polygons") from an object of type "NULL"
mun.br
, I assume)? Could it happen if there's no match to a CODIGO_MUN and somun
in the first line of the loop is NULL? Hard to tell without your data....