0

I obtain the below error when I try to coerce my raster to a vector.

Initially my rasters are of different extents and I use the projectRaster tool to obtain the same extent for both the rasters. However, when I coerce the two to a Spatial Pixels Data Frame in R, I get different number of elements. Why is this happening?

> bio5
 class       : RasterLayer 
 dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
 resolution  : 0.04166667, 0.04166667  (x, y)
 extent      : 72.45835, 82.70835, 8.083337, 22.20834  (xmin, xmax, ymin, ymax)
 coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
 data source : in memory
 names       : bio_5 
 values      : 20.7, 43.2  (min, max)

Before Projection:

> lg5
class       : RasterLayer 
dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
resolution  : 0.04166667, 0.04166667  (x, y)
extent      : 72.45833, 82.70833, 8.083333, 22.20833  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : cclgmbi5 
values      : 18.7, 39.2  (min, max)

After Projection:

new.lg5 <- projectRaster(lg5,bio5)
> new.lg5
 class       : RasterLayer 
 dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
 resolution  : 0.04166667, 0.04166667  (x, y)
 extent      : 72.45835, 82.70835, 8.083337, 22.20834  (xmin, xmax, ymin, ymax)
 coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
 data source : in memory
 names       : cclgmbi5 
 values      : 18.70035, 39.2  (min, max)

Coercing them to a vector:

 > x <- as(bio5,"SpatialPixelsDataFrame")
 > y <- as(new.lg5, "SpatialPixelsDataFrame")
 > str(x)
  Formal class 'SpatialPixelsDataFrame' [package "sp"] with 7 slots
  ..@ data       :'data.frame': 54202 obs. of  1 variable:
  .. ..$ bio_5: num [1:54202] 40.3 40.3 40.3 40.4 40.4 40.3 40.2 40.2 40.3 40.3 ...
  ..@ coords.nrs : num(0) 
  ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
  .. .. ..@ cellcentre.offset: Named num [1:2] 72.5 8.1
  .. .. .. ..- attr(*, "names")= chr [1:2] "s1" "s2"
  .. .. ..@ cellsize         : num [1:2] 0.0417 0.0417
  .. .. ..@ cells.dim        : int [1:2] 246 339
  ..@ grid.index : int [1:54202] 4 5 6 8 9 10 11 12 13 14 ...
  ..@ coords     : num [1:54202, 1:2] 72.6 72.6 72.7 72.8 72.8 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x" "y"
  ..@ bbox       : num [1:2, 1:2] 72.5 8.08 82.71 22.21
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

  > str(y)
   Formal class 'SpatialPixelsDataFrame' [package "sp"] with 7 slots
  ..@ data       :'data.frame': 54747 obs. of  1 variable:
   .. ..$ cclgmbi5: num [1:54747] 37.6 37.4 37.4 37.4 37.4 ...
  ..@ coords.nrs : num(0) 
  ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
   .. .. ..@ cellcentre.offset: Named num [1:2] 72.5 8.1
   .. .. .. ..- attr(*, "names")= chr [1:2] "s1" "s2"
  .. .. ..@ cellsize         : num [1:2] 0.0417 0.0417
  .. .. ..@ cells.dim        : int [1:2] 246 339
  ..@ grid.index : int [1:54747] 3 4 5 6 7 8 9 10 11 12 ...
  ..@ coords     : num [1:54747, 1:2] 72.6 72.6 72.6 72.7 72.7 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x" "y"
  ..@ bbox       : num [1:2, 1:2] 72.46 8.08 82.71 22.21
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
  • @Vijay_Ramesh, please provide the clipped data, that would be helpful for an answer. – blabbath Mar 17 '17 at 12:28
  • I'm aware this is not providing an answer and it can gladly be turned into a comment. Yet I might know an answer if I could play around with the data a little. Sorry for the inconvinience, I'm working on the reputation. – blabbath Mar 17 '17 at 13:06
  • No problem at all. I completely understand :). As I mentioned in the comment to Edzer below and I have linked the data as well. All I did was download the data and clipped it. BUT, I also realized that the two datasets before clipping differ in their extent at ymin by the 6th decimal place bio - 90.00001, and lgm - 90. So, I resampled and ensured that the extents are the same, following which I clipped it. But, I realized as Edzer mentioned that when your coerce to a SpatialPixels Dataframe, it keeps only those rows that have NO missing values. Feel free to take a look. – Vijay Ramesh Mar 17 '17 at 13:41
1

Since you don't give the data, I can only guess, but my guess is that your raster contains missing values; SpatialPixelDataFrame objects ignore raster cells with missing values for all attributes/layers.

  • The data am using is the raw data downloaded from the WorldClim website for Current Climate and Last Glacial Maximum (at resolution of 2.5arc min). I downloaded bio5 for both time frames to start with and I clipped it with a mask for my region. You might be right that, they could have missing values. Any suggestions? (Can provide clipped data if you want to take a look?) worldclim.org/version1 – Vijay Ramesh Mar 16 '17 at 13:21
  • I took a look at the raster and yup, it's replacing all rows with missing data. Thanks for the answer! – Vijay Ramesh Mar 17 '17 at 13:22

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.