Reputation: 43
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.
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.
I have no idea how to do this, so if you have any suggestions please share.
Here is an example of what my data looks like:
#yearly vegetation trend residuals
res <- list.files(pattern="*res.tif")
s <- stack(res)
> 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 <- df[6:11,4:35]
> 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
>
Upvotes: 2
Views: 2298
Reputation: 390
Although this is too late to answer the question above, I think it is worth answering in case someone else have a similar issue.
For this kind of problem, I think you can filter your vector keeping only years that you want to make a new raster stack. I think one can manipulate this part based on the data type and intention they have. All they need is to have a vector of years. After that they can follow the following methods to subset the corresponding rasters.
sub <- raster::subset(rastStack, index) # here, rastStack is the raster stack and index is the vector of years, however the layers of raster stack should have year in the layer name.
The following method can be applied, if the intention is to make a raster from the stack of every ith rasters from a raster stack. For example, I have a raster stack containing monthly mean precipitation values for 49 years. What I want is to make a mean raster for every month over the period such that I will have 12 rasters only as a mean of 49 years. Which means that my 1st raster will be the mean value of January using mean of all January months over 49 years, the same is for other months and so on.
rasList <- list.files("your path of folder here", pattern = ".tif$", full.names = T) # take all rasters from a folder
rastStack <- stack(rasList)
mon <- seq(1,12, 1) # vector representing months
li <- 1:588 # nlyears in the raster stack. 12 month in a year, I had rasters for 49 years. therfore a total of 12*49 or 588 raster layers
for (ii in 1:length(mon)){
index <- li[seq(ii, length(li), 12)]
sub <- raster::subset(rastStack, index)
subMean <- calc(sub, fun = mean) # this can be any function
writeRaster(subMean, paste("your destination folder to save the output rasters", "month_", ii, ".tif", sep = ""))
}
Alternatively, anyone may have intention to make a raster stack by sequence. For example, the person may want to make a raster stack for every nth layer. Which means that the first group of stack can be first 10 rasters, second group of stack can be second 10 rasters and so on. In that case the can be found in this post.
Upvotes: 1