0

I've been trying to extract bioclimatic variables from WorldClim based on a lat-long data frame. I've successfully obtained the RasterStack but when trying to extract the variables from a subset of coordinates ('coords' object in the code), I obtained a lot (but not all) of NAs. Does anyone know what could be the problem?

Here is my code:

library(raster)
library(sp)
r <- getData("worldclim",var="bio",res=2.5)
r <- r[[c(1,4, 10, 11, 12, 15, 16, 17)]]
names(r) <- c("Temp", "Temp_seasonality", "Temp_warmest_q","Temp_coldest_q","Prec", "Prec_seasonality", "Prec_wettest_q", "Prec_driest_q")

coords looks like:

          [,1]     [,2]
  [1,]  14.650   40.350
  [2,]  13.600   39.350
  [3,]   0.033  -78.717
  [4,]  37.017   -2.383
  [5,]  46.837 -113.966

dim(coords)
[1] 421   2

points <- SpatialPoints(coords, proj4string = r@crs)
values <- extract(r,points)

values df has a lot of rows with NAs, although some of them do have the bioclimatic variables.

  • Did you plot results? Probably are out of valid data – aldo_tapia Aug 7 '19 at 22:34
  • I did. The problem had to do with the fact that the columns of 'coords' were reversed as indicated by Spacedman below. Thank you! – user147444 Aug 8 '19 at 0:44
0

Your coords are:

          [,1]     [,2]
  [1,]  14.650   40.350
  [2,]  13.600   39.350
  [3,]   0.033  -78.717
  [4,]  37.017   -2.383
  [5,]  46.837 -113.966

but coordinate matrices are always X then Y, ie Longitude then Latitude. Your NAs are coming from column 2 being more than 90 or less than -90, which are not valid latitudes.

extract(r, coords[,2:1]) should be sufficient to flip the column order (you don't really need to construct a spatial points object because extract is flexible).

| improve this answer | |
  • It worked with extract(r, coords[,2:1]). Thank you very much! – user147444 Aug 8 '19 at 0:26

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.