2

I'm working with a shapefile of national parks in Costa Rica, and I'm trying to get rid of the marine reserves and national parks consisting of water.

To download the dataset:

#create data driectory
    if (!dir.exists('data')) {
  dir.create('data')
}

#download dataset from ProtectedPlanet
nps_URL <- 'https://www.protectedplanet.net/downloads/WDPA_Feb2020_search_costa_rica_Costa_Rica,National_P_bf6ceea4036fb52f7e08497edb2f615999e7fffcbc1e2335dd6dbb78f3fd06a5?type=shapefile'
nps_filename <- './data/WDPA_Feb2020_search_costa_rica_Costa_Rica,National_P_bf6ceea4036fb52f7e08497edb2f615999e7fffcbc1e2335dd6dbb78f3fd06a5-shapefile-polygons.shp'
if (!file.exists(nps_filename)) {
  download.file(url = nps_URL, destfile = './data/nps.zip', method = 'auto')
  unzip('./data/nps.zip', exdir = './data')
}

#load the national parks as 'natparks'
natparks <- st_read(dsn = nps_filename)

Next, I have a vector containing the name strings of the national parks to be removed:

marinereserves <- c("Marino Ballena","Cahuita","Manuel Antonio","Tortuguero","Santa Rosa","Isla del Coco","Marino las Baulas de Guanacaste")

Then when trying to remove them, I thought of simply subsetting them by:

natparks_sub <- natparks[natparks$NAME == marinereserves,,drop=TRUE]

However, this removes nearly all national parks. Strangely enough it only keeps 2 parks, both of them are also in the marinereserves vector.

How can I properly remove the mentioned national parks from the simple feature collection?

1

You need to test if any of the park NAME attributes are in the list of reserves:

> natparks_sub <- natparks[natparks$NAME %in% marinereserves,,drop=TRUE]
> dim(natparks_sub)
[1]  7 29

Using == here tests each element of NAME with each element of marinereserves in turn. You would have got a warning if not for the chance that you have 7 marine reserve names and 28 national parks. EG if I drop the first park so there's only 27...

> natparks_sub <- natparks[natparks[-1,]$NAME == marinereserves,,drop=TRUE]
Warning messages:
1: In is.na(e1) | is.na(e2) :
  longer object length is not a multiple of shorter object length
2: In `==.default`(natparks[-1, ]$NAME, marinereserves) :
  longer object length is not a multiple of shorter object length

This all goes back to R "recycling" shorter vectors when doing vector-vector operations:

> (1:12) == (1:3)
 [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> (1:12) == (1:7)
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
Warning message:
In (1:12) == (1:7) :
  longer object length is not a multiple of shorter object length

but you need to use %in% when testing for inclusion.

| improve this answer | |
  • Thanks a lot! That helped and was interesting to learn as well. – saQuist Jan 27 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.