How to subset a spatial polygon in R by matching partial strings?
Asked Answered
F

0

0

I wanna divide a polygon shapefile (deforestation in brazilian Amazon), by years of deforestation. The years are in a string field, like "d2010_1", "d2010_2", "d2011_1" and so on. I want to divide it in 5 year periods. 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 it 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 it gave the same error message. I've seen some examples like here, here and here, but I need to see if the beginning of the string matches, not numerical operators such as <, > or ==. Any help, please?

EDIT: I figured out a way, but something strange is happening. I did 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 between 2000 and 2004. So far so good. But when I ask to see the second variable, it shows the 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 were supposed to stay out (and were out of the listing). What's happening?

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

Floorer answered 1/8, 2013 at 20:3 Comment(5)
What is the output of sapply( prodes@data , class )? Is CLASS_NAME a factor variable? I also think you need to replace the || with |.Provencal
Yes, it is a factor variable.Floorer
Also, using the strategy above, with d00a04 <- subset(prodes,prodes@data$CLASS_NAME %in% anos00a04) also worked. I'm just wondering why still 49 levels...Floorer
Because R doesn't automatically drop unused factor levels. You need to refactor... d00a04@data$CLASS_NAME <- factor(d00a04@data$CLASS_NAME) and unused factor levels will disappearProvencal
Don't use prodes@data$CLASS_NAME, just use prodes$CLASS_NAMESaguenay

© 2022 - 2024 — McMap. All rights reserved.