Manuel,

Given that your layers also have different resolutions, I think you need to
use resample(). Since you need to set the resolutions the same to stack the
rasters, give it some thought to make sure you are doing what your really
want - which resolution is more appropriate and such, and which resampling
procedure makes the most sense.

This is assuming your two layers are in the same projection (in your
dataset, it looks like the projection for 'distpob_b' is not set). If your
layer 'distpob_b' is in the same projection as 'forestRaster', you can make
sure the projection is set the same by using:
projection(distpob_b) <- projection(forestRaster)

Then, use resample() to make them the same resolution and dimensions:
distpobResamp <- resample(distpob_b, forestRaster, resample='bilinear') # I
set it so the result would be the same size as the layer with the smaller
extent

s<-stack(distpobResamp, forestRaster) #creates stack, 's'

If the projections are different, you can first set the projection of
distpob_b using the projection() <- command, and then use projectRaster to
transform it so they are in the same projection, and you can simultaneously
resample them/set the same resolution. Again, think about which
reprojection is best, and if you're dealing with lots of layers, it may be
best to us the most common projection, as to avoid errors caused by
resampling.


Here's a full example with made-up data, but using the same
resolutions/extents as your data:


library(raster)

##Create raster 'r1' (same dimensions and resolution as distpob_b)
r1 <- raster(xmn=447181, xmx=517981, ymn=1121851, ymx=1216051,
crs=CRS('+proj=tmerc +lat_0=0 +lon_0=-84 +k=0.9999 +x_0=500000 +y_0=0'))#
res=300)
res(r1) <- 300
values(r1) <- runif(ncell(r1)) #Fill with values
#plot(r1)

##Create raster 'r2' (same dimensions and resolutoin as forestRaster)
r2 <- raster(xmn=447235.4, xmx=517235.4, ymn=1122046, ymx=1216046,
crs=CRS('+proj=tmerc +lat_0=0 +lon_0=-84 +k=0.9999 +x_0=500000 +y_0=0'))
res(r2) <- 500
values(r2) <- runif(ncell(r2))
#plot(r2)

##Resample r1 to extent/resolution of r2
r1rsmp <- resample(r1,r2, resample='bilinear')

##Create stack 'r1r2stk'
r1r2stk <- stack(r1rsmp, r2)

##Plot them to make sure it looks right...
#plot(r1r2stk)

Hope that helps,
Mike




On Mon, Oct 21, 2013 at 4:11 PM, Manuel Spínola <mspinola10@gmail.com>wrote:

> Dear list members,
>
> I am trying to stack 2 rasters, but I got an error:
>
> > nd <- stack(forestRaster, distpob_b)
> Error in compareRaster(x) : different extent
>
> > show(forestRaster)
> class       : RasterLayer
> dimensions  : 188, 140, 26320  (nrow, ncol, ncell)
> resolution  : 500, 500  (x, y)
> extent      : 447235.4, 517235.4, 1122046, 1216046  (xmin, xmax, ymin,
> ymax)
> coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-84 +k=0.9999 +x_0=500000 +y_0=0
> +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
> data source : in memory
> names       : layer
> values      : 0, 100  (min, max)
>
> > show(distpob_b)
> class       : RasterLayer
> dimensions  : 314, 236, 74104  (nrow, ncol, ncell)
> resolution  : 300, 300  (x, y)
> extent      : 447181, 517981, 1121851, 1216051  (xmin, xmax, ymin, ymax)
> coord. ref. : NA
> data source : in memory
> names       : layer
> values      : 5.644242, 9530.715  (min, max)
>
>
> How I make 2 raster with equal extent?
>
> Best,
>
> Manuel
>
> --
> *Manuel Spínola, Ph.D.*
> Instituto Internacional en Conservación y Manejo de Vida Silvestre
> Universidad Nacional
> Apartado 1350-3000
> Heredia
> COSTA RICA
> mspinola@una.ac.cr
> mspinola10@gmail.com
> Teléfono: (506) 2277-3598
> Fax: (506) 2237-7036
> Personal website: Lobito de río <
> https://sites.google.com/site/lobitoderio/>
> Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
>
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>

	[[alternative HTML version deleted]]

