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.