title: Select specific rasters from stack and make a new stack in R
body: <p>I have a stack of vegetation trend rasters with one layer for each year between 1982-2013. Furhermore I have some data in a vector that I would like to compare to the rasters. Unfurtunalety this vector data is not complete and some years have no values. </p>
  
  <p>Is there a way to make a new stack with only the rasters corresponding to the years where my vetor data has values? I have 180 vectors, so I would love if I could avoid doing this manually for each vector.</p>
  
  <p>I have no idea how to do this, so if you have any suggestions please share. </p>
  
  <p>Here is an example of what my data looks like:</p>
  
  <pre><code>#yearly vegetation trend residuals
  res &lt;- list.files(pattern="*res.tif")
  s &lt;- stack(res)
  
  &gt; s
  class       : RasterStack 
  dimensions  : 2160, 4320, 9331200, 32  (nrow, ncol, ncell, nlayers)
  resolution  : 0.08333333, 0.08333333  (x, y)
  extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
  coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
  names       :  X1982res,  X1983res,  X1984res,  X1985res,  X1986res,  X1987res,  X1988res,  X1989res,  X1990res,  X1991res,  X1992res,  X1993res,  X1994res,  X1995res,  X1996res, ... 
  min values  : -1646.886, -1783.155, -1597.452, -1296.460, -1339.577, -1396.953, -1147.745, -1135.821, -1245.651, -1239.484, -1271.816, -1326.402, -1119.192, -1251.991, -1137.646, ... 
  max values  :  619.8672,  602.8412,  616.0391,  654.5453,  506.0248,  569.0380,  602.9570,  605.4675,  642.4062,  571.4812,  631.8979,  604.1268,  581.0545,  692.8349,  697.4185, ... 
  
  #table with vectors
  vectors &lt;- df[6:11,4:35]
  &gt; vectors
     X1982 X1983 X1984 X1985    X1986 X1987 X1988    X1989   X1990 X1991
  6     NA    NA    NA    NA 26.90000    NA  28.0 25.85000 26.9000 29.60
  7   24.5 25.10  25.1 28.15 35.85714 27.95  30.6 30.28750 37.6625 30.05
  8     NA 31.00    NA    NA       NA 27.15    NA       NA      NA 26.30
  9     NA    NA    NA    NA 26.60000    NA  31.7 30.36667 34.5000    NA
  10    NA    NA    NA    NA 48.10000    NA  40.6 44.50000      NA 41.80
  11    NA 34.35    NA 26.92 31.87500    NA  28.2 38.00000      NA 27.57
     X1992 X1993 X1994  X1995    X1996  X1997 X1998 X1999    X2000    X2001
  6  35.50 36.60  32.1 38.100 52.64000     NA  46.0 36.22 48.60000 35.36000
  7  30.65 30.85  30.4 37.810 34.82500 34.775  39.2 33.15 36.88333 32.44800
  8     NA    NA  28.0 27.350 26.00000 25.800  24.0 26.00 24.90000 24.00000
  9  36.10    NA  42.8 40.275 45.80000 46.200  46.2    NA 40.35000 41.28333
  10 41.90 45.30    NA     NA       NA     NA    NA    NA       NA 57.50000
  11 32.65    NA    NA 32.940 39.23333     NA    NA    NA 33.41000       NA
     X2002    X2003  X2004  X2005   X2006  X2007   X2008  X2009   X2010 X2011
  6  35.34 45.24333 41.505 39.695 36.2450 30.015 32.3050 32.540 33.6350 34.21
  7  30.90 32.65500 35.800     NA      NA     NA 36.1660     NA 35.3680 28.30
  8     NA 27.40000 31.054 31.855 32.3725 32.695 32.8625 33.675 33.2875 28.80
  9  34.08 18.81000 16.230 16.640      NA     NA 31.7900     NA      NA    NA
  10    NA 48.00000 43.000     NA      NA     NA      NA     NA      NA    NA
  11    NA       NA     NA 41.300      NA     NA      NA     NA 41.5575    NA
     X2012 X2013
  6  30.48 31.54
  7  32.40    NA
  8  29.04 27.00
  9     NA    NA
  10    NA    NA
  11    NA    NA
  &gt; 
  </code></pre>