3

I am trying to create a species distribution model in R. I have created raster layers in ArcMap and have imported them into R. They cannot be stacked unless the extents are exactly the same and they all have the same number of rows and columns.

However, when I alter these factors to successfully stack them they lose all their values and my stacked data frame is just filled with NAs.

Does anyone know how I can alter the extent and resolution of my raster layers so they can be successfully stacked -- so I can then attach environmental info to presence points.

Cheers

StuartDrew
  • 31
  • 1
  • 3
  • 1
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Heroka Aug 27 '15 at 14:48
  • To see a list of the many functions that'll help you wrestle your data into shape (including `merge()`, `mosaic()`, `crop()`, `extend()`, `trim()`, `aggregate()`, `disaggregate()`, `resample()`, .......) type: `library(raster); help("raster-package")`, and check out the Section 2, "Changing the spatial extent and/or resolution of Raster* object." If you need more functionality than is available there, perhaps see also the **gdalUtils** package, though in most cases the **raster** package should have you well covered. – Josh O'Brien Aug 27 '15 at 16:04

2 Answers2

4

One way to do this is to choose a raster that has the projection and extent that you want and use that as a template for the others

For example, if you have rasterA and rasterB. You can use projectRaster() to make a new version of rasterA with the same extent and resolution as rasterB. You should then be able to stack new.rasterA & rasterB.

new.rasterA <- projectRaster(rasterB, rasterA) # define the projection and extent

r.stack <- stack(new.rasterA, rasterB)   # add them to a raster stack object
prosoitos
  • 6,679
  • 5
  • 27
  • 41
NRP
  • 503
  • 1
  • 4
  • 6
  • Don't you need a crop() function to set the extent? – FraNut Feb 03 '16 at 10:42
  • I think this should be `new.rasterA <- projectRaster(rasterB, rasterA)` since it is `projectRaster(from, to...)`. Edit: I just submitted a correction. – prosoitos May 28 '19 at 21:53
0

I had the same issue and I solved this in arcgis by snapping each raster to a mask of my study area.

This can be done by clicking geoprocessing -> environments -> processing extent - then select a layer you want to snap to in the snap raster box. I did this before I extracted (clipped) each layer and it worked perfectly. You can check the extent in properties when you are done for each layer you do to double check before you upload them into R.