0

I have a list of coordinates from recorded sightings for a plant species.

I am extracting mean annual temperature from a map at each coordinate (using raster::extract).

But I now realise that a few coordinates are in the ocean due to some sampling error (e.g. they are just off the coast), causing the extracted values from maps to be inaccurate, because the map only gives information about terrestrial sites.

Is there a quick way of removing ocean coordinates form the list of coordinates, or ignoring ocean coordinates when extracting temperature values from the map? (I have to do this for many species and environmental variables)

Thank you

loki
  • 9,816
  • 7
  • 56
  • 82
cflax
  • 63
  • 6
  • 1
    Help others reproduce your problem. Please read this https://stackoverflow.com/help/how-to-ask – rar Nov 01 '18 at 05:22
  • also have a look at [how to ask a great R question](https://stackoverflow.com/q/5963269/3250126) – loki Nov 01 '18 at 07:35
  • Without knowing your exact data and site, I would use additional data sets, which allow for exclusion of such incorrect coordinates. One that comes to mind would be the GADM data. If you work only in one (or few) countr(y/ies), this is very easily done by using `getData('FRA', 'GADM')` (here: France) and subsetting your points with the administrative boundaries. However, keep in mind that is not that accurate. Another possibility would be to use OSM coastline polygon data. – loki Nov 01 '18 at 07:41
  • Further, think about moving this question over to https://gis.stackexchange.com – loki Nov 01 '18 at 07:42

1 Answers1

0

You can use na.omit or similar after you extract the values and detect the NAs

Example data:

library(raster)
set.seed(1)
r <- raster(ncol=36, nrow=18, vals=1:(18*36))
r[sample(ncell(r), 300)] = NA
s <- stack(r, r)
names(s) <- c('temperature', 'rain')
xy <- cbind(lon=-50, lat=seq(-80, 80, by=20))
sp <- data.frame(sp=rep(c('A', 'B', 'C'), 3), xy)

Use extract

e <- extract(s, xy)
x <- data.frame(sp, e)
x
#  sp lon lat temperature rain
#1  A -50 -80          NA   NA
#2  B -50 -60          NA   NA
#3  C -50 -40         482  482
#4  A -50 -20          NA   NA
#5  B -50   0         338  338
#6  C -50  20         266  266
#7  A -50  40          NA   NA
#8  B -50  60         122  122
#9  C -50  80          NA   NA

y <- na.omit(x)
y
#  sp lon lat temperature rain
#3  C -50 -40         482  482
#5  B -50   0         338  338
#6  C -50  20         266  266
#8  B -50  60         122  122
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63