0

I got a RasterStack that has got following description:

class      : RasterStack 
dimensions : 221, 121, 26741, 14976  (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.25  (x, y)
extent     : 14.875, 45.125, 24.875, 80.125  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 
names      : index_1979.01.01, index_1979.01.02, index_1979.01.03, index_1979.01.04, index_1979.01.05, index_1979.01.06, index_1979.01.07, index_1979.01.08, index_1979.01.09, index_1979.01.10, index_1979.01.11, index_1979.01.12, index_1979.01.13, index_1979.01.14, index_1979.01.15, ... 
min values :         46604.85,         47328.07,         48944.12,         49320.65,         49244.67,         49516.16,         49504.01,         48959.65,         48608.90,         47603.10,         47572.72,         48564.15,         49816.92,         49078.65,         48321.72, ... 
max values :         57006.81,         56968.60,         56958.67,         56976.26,         57288.55,         57535.62,         57659.48,         57581.33,         57381.65,         57052.99,         56803.95,         56854.89,         56783.50,         56739.44,         56600.52, ... 

It has 14 975 layers, every day since 1.1.1979 till 31.12.2019. Now, I would like to get 12 Stacks out of with respect to month, so I wanna subset it into 12 smaller stacks. As I had troubles to change names of layers of the stack properly, I thought of another way to do it. I made a vector with the same amount of characters as the stacks layers, the first 31 characters are named JAN, another 28 FEB and so on...I did it this way:

n<-names(stack)
nn<-substr(n,12,13)
nn<-gsub('01','JAN',nn)
nn<-gsub('02','FEB',nn)
...
nn<-gsub('12','DEC',nn).

And now I want to subset the stack by this vector nn, something like this: sub<-raster::subset(stack,nn).

I hope you understand what I want to do. Every help appreciated, thanks!

Lime
  • 738
  • 5
  • 17

2 Answers2

1

You can try this:

layer_name <- names(stack)
layer_name <- str_remove_all(layer_name, "[index_]")

#install.packages("lubridate")
library(lubridate)

layer_name <- ymd(layer_name)

#Create an indices to prepare it for stackApply, which takes the means for all the days of the month within each year.
indices <- format(as.Date(layer_name, format = "%Y.%m.%d"), format = "%Y.m") 

raster_mean <- stackApply(stack, indices, mean)
  • layer_name <- names(stack) this takes the names from the raster in preparation for taking the sum or means for the months
  • str_remove_all() this removes the first words from the names index_ to filter it into dates -ymd() turns the characters into date format for filtering
  • Indices selects the date format of years and months
  • stackApply()Using indices, it takes all the days within each month from the raster and takes the mean values
Lime
  • 738
  • 5
  • 17
0
n <-names(stack)
n <- c("index_1979.01.01", "index_1979.01.02", "index_1979.01.03", "index_1979.01.04", "index_1979.01.05", "index_1979.01.06", "index_1979.01.07", "index_1979.01.08", "index_1979.01.09", "index_1979.01.10", "index_1979.01.11", "index_1979.01.12", "index_1979.01.13", "index_1979.01.14", "index_1979.01.15")

nn <- as.integer(substr(n,12,13))

Now to get the layers for January

sjan <- stack[[nn == 1]] 

Or with subset

subjan <-raster::subset(stack, which(nn==1))
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63