title: How to create a raster brick with rasters of different extents?
body: <p>I am new in <strong>R</strong> so this question is very basic but I have been struggling with it and could not find a solution that worked. I want to create a raster brick from some landsat images of the same area.
  They were downloaded in <code>HDF-EOS</code> format, and I used the <em>Modis Reprojection Tool</em> to convert them to <code>.tif</code>.</p>
  
  <p>The resulting rasters have the same projection, but differ in their extent, resolution and origin.</p>
  
  <p>I tried several approaches, summarized here below:</p>
  
  <ol>
  <li><p>defining a subset extent manually and subsetting all the rasters. Then trying to make a brick with the subset rasters</p></li>
  <li><p>Resampling the rasters, to give them the same number of columns and rows. Ideally that would ensure the raster cells are aligned and can be put into a raster brick. This option created a brick where rasters had no values, they were empty.</p></li>
  </ol>
  
  <p>I am wondering what is the concept I should follow to correct the extent. Would it be correct (and efficient) to create an empty raster that I would fill in later with the values of the imported landsat image? Can you see where I am making a mistake?
  In case it is relevant, I am working on a <em>Mac OSX Version 10.9.1</em>, and using <em>rgdal version 0.8-14</em></p>
  
  <p>Any help will be very appreciated! </p>
  
  <p>Thankyou</p>
  
  <p>I add here the code I have been using:</p>
  
  <pre><code># .tif files have been creating using the Modis Reprojection Tool. Input
  # files used for this Tool was LANDSAT HDF-EOS imagery.
  
  library(raster)
  library(rgdal)
  
  setwd()=getwd()
  
  # Download the files from dropbox:
  dl_from_dropbox &lt;- function(x, key) {
    require(RCurl)
    bin &lt;- getBinaryURL(paste0("https://dl.dropboxusercontent.com/s/", key, "/", x),
                        ssl.verifypeer = FALSE)
    con &lt;- file(x, open = "wb")
    writeBin(bin, con)
    close(con)
    message(noquote(paste(x, "read into", getwd())))
  }
  dl_from_dropbox("lndsr.LT52210611985245CUB00-vi.NDVI.tif", "qb1bap9rghwivwy")
  dl_from_dropbox("lndsr.LT52210611985309CUB00-vi.NDVI.tif", "sbhcffotirwnnc6")
  dl_from_dropbox("lndsr.LT52210611987283CUB00-vi.NDVI.tif", "2zrkoo00ngigfzm")
  
  
  
  # Create three rasters
  tif1 &lt;- "lndsr.LT52210611985245CUB00-vi.NDVI.tif"
  tif2 &lt;- "lndsr.LT52210611985309CUB00-vi.NDVI.tif"
  tif3 &lt;- "lndsr.LT52210611987283CUB00-vi.NDVI.tif"
  
  r1 &lt;- raster(tif1, values=TRUE)
  r2 &lt;- raster(tif2, band=1, values=TRUE)
  r3 &lt;- raster(tif3, band=1, values=TRUE)
  
  ### Display their properties
  # projection is identical for the three rasters
  projection(r1)
  # "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
  projection(r2)
  projection(r3)
  
  # Extents are different
  extent(r1)
  # class       : Extent 
  # xmin        : -45.85728 
  # xmax        : -43.76855 
  # ymin        : -2.388705 
  # ymax        : -0.5181549
  extent(r2)
  # class       : Extent 
  # xmin        : -45.87077 
  # xmax        : -43.78204 
  # ymin        : -2.388727 
  # ymax        : -0.5208711 
  extent(r3)
  # class       : Extent 
  # xmin        : -45.81952 
  # xmax        : -43.7173 
  # ymin        : -2.405129 
  # ymax        : -0.5154312
  
  # origin differs for all
  origin(r1)
  # 5.644590e-05 -8.588605e-05
  origin(r2)
  # 0.0001122091 -0.0001045107
  origin(r3)
  # 6.949976e-05 -5.895945e-05
  
  # resolution differs for r2
  res(r1)
  # 0.0002696872 0.0002696872
  res(r2)
  # 0.0002696875 0.0002696875
  res(r3)
  # 0.0002696872 0.0002696872
  
  
  ## Try different approaches to create a raster brick
  
  # a- define a subset extent, and subset all the rasters
  plot(r1, main="layer1 NDVI")
  de &lt;- drawExtent(show=TRUE, col="red")
  de
  # class       : Extent 
  # xmin        : -45.36159 
  # xmax        : -45.30108 
  # ymin        : -2.002435 
  # ymax        : -1.949501
  e &lt;- extent(-45.36159,-45.30108,-2.002435,-1.949501)
  # Crop each raster with this extent
  r1c &lt;- crop(r1,e)
  r2c &lt;- crop(r2,e)
  r3c &lt;- crop(r3,e)
  # Make raster brick
  rb_a &lt;- brick(r1c,r2c,r3c)
  # Error in compareRaster(x) : different extent
  
  # b- Resample each raster
  s &lt;- raster(nrow=6926, ncol=7735)  # smallest nrow and ncol among r1,r2 and r3
  r1_res &lt;- resample(r1,s, method="ngb")
  r2_res &lt;- resample(r2,s, method="ngb")
  r3_res &lt;- resample(r3,s, method="ngb")
  # Resampling gives for the three rasters the following message:
  # Warning message:
  #   In .local(x, y, ...) :
  #   you are resampling y a raster with a much larger cell size, 
  #   perhaps you should use "aggregate" first
  
  # Make raster brick
  rb_c &lt;- brick(r1, r2, r3)
  # Error in compareRaster(x) : different extent
  </code></pre>