How to multiply a spatial polygon in R by matching partial rows?

I want to split a polygon shape file (deforestation in the Brazilian Amazon), over the years of deforestation. Years are in the row field, for example, "d2010_1", "d2010_2", "d2011_1" and so on. I want to divide it into 5 years. I tried the following:

d00a04 <- prodes[grepl("d2000",prodes@data$CLASS_NAME) || grepl("d2001",prodes@data$CLASS_NAME) || grepl("d2002",prodes@data$CLASS_NAME) || grepl("d2003",prodes@data$CLASS_NAME) || grepl("d2004",prodes@data$CLASS_NAME),] 

but he gave the following error:

 Error in if (is.numeric(i) && i < 0) { : missing value where TRUE/FALSE needed 

I also tried:

 anos00a04 = c("d2000","d2001","d2002","d2003","d2004") d00a04 <- subset(prodes,prodes@data$CLASS_NAME %in% anos00a04) 

but he gave the same error message. I saw several examples, for example here , here and here , but I need to see if the beginning of the line matches, and not the numerical operators such as <,> or ==. Any help please?

EDIT: I figured out the way, but something strange is happening. I have done the following:

 anos <- sort(unique(prodes@data$CLASS_NAME)) anos00a04 <- anos[2:20] 

The first command gives me all 49 levels from the original shapefile. The second returns only those that were between 2000 and 2004. So far so good. But when I ask to see the second variable, it shows 19 itens (d2000_2 d2000_3 d2001_0 d2001_3 d2001_4 ...), but below it says: "49 levels: d1997_0 d2000_2 d2000_3 ...", including those that should have remained ( and were excluded from the list). What's happening?

PS: "anos" is the Portuguese word for years.

0
string-matching r geospatial
Aug 01 '13 at
source share

No one has answered this question yet.

See similar questions:

fifty
A simple way to subset a SpatialPolygonsDataFrame (i.e. Delete polygons) by attribute in R
four
In R, how do I join a subset of SpatialPolygonsDataFrame?

or similar:

7432
How to check if a string contains a substring in JavaScript?
2664
How to check if a string contains a specific word?
229
Check if the string matches the pattern
one
A subset of spatial points with a polygon
one
Convert spatial lines to spatial polygons
one
Separation of individual spatial polygons equally in R
one
R: Allied spatial polygons will not completely remove the dividing line
one
How to create a reactive glossy map with a spatial polygon
0
Set of spatial polygons + subset
0
Derived spatial lines indicating irregular polygons



All Articles