0

I got myself a rasterbrick called y, which has got 14 975 time layers as its got values of daily mean geopotential heigth every day since 1.1.1979 till 31.12.2019 (14 975 days). The brick has following description:

class      : RasterBrick 
dimensions : 221, 121, 26741, 14975  (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 
source     : C:/Users/Adam/AppData/Local/Temp/RtmpaKZVdb/raster/r_tmp_2020-10-26_165849_53084_29346.grd 
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, ... 

and I would like to subset this rasterbrick into 12 rasterbricks by month so that I had 1 rasterbrick for every calendar month. I tried to do that several ways but nothing worked out well. For example, I tried to substract month character from names(y), and I think its definitely the way to go but it simply does not work. Every help appreciated, thank you!

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • What did you try? Please provide a reproducible example, and show some code. How is this question different from your earlier question https://stackoverflow.com/questions/64514678/calculating-long-term-daily-means-from-a-raster-in-r/64516961#64516961 – Robert Hijmans Oct 27 '20 at 18:05
  • I tried to set the names of the rasterlayer so that there was only a number indicating month of each day. But it doesnt work, there is a problem to do this, I did it various way but R either adds X before, or adds weird number to the ending of names. So now I made a vector named ww which has got 14 975 characters, first 31 is JAN, another 28 FEB and so on. And now, I would like to subset the stack by this vector, if it is possible, something like x<-raster::subset(y, ww). But I dont now exactly how to do that. – Adam Dragula Oct 27 '20 at 19:43

2 Answers2

0

if you try with this:

# We changed the names of the layers to months:

layer_names_original <- names(y)
layer_names <- layer_names_original

layer_names_2 <- gsub('index_', '', layer_names)
layer_names_3 <- gsub('\\.', '-', layer_names_2)
layer_names_3_as_date <- as.Date(layer_names_3)

library(lubridate)
layer_names_3_in_months <- month(layer_names_3_as_date)
names(y) <- layer_names_3_in_months

# We filtered the layers of the month 'i'
i <- 1 # january
y_i <- y[[i]] 

# We replaced the month names by the originals
id_match <- which(names(y)%in%i)
names(y_i) <- layer_names_original[id_match]


# Pd: Try changed brick by stack in case of problems (y <- stack(y))
0

You try with this:

layer_names <- names(y)
layer_names_2 <- gsub('index_', '', layer_names)
layer_names_3 <- gsub('\\.', '-', layer_names_2)
layer_names_3_as_date <- as.Date(layer_names_3)

library(lubridate)
layer_names_3_in_months <- month(layer_names_3_as_date)

# We filtered the layers of the month 'i'
i <- 1 # january
layer_names_3_in_months_i <- which(layer_names_3_in_months==i) 
layer_month_i <- names(y)[layer_names_3_in_months_i]

# Filter done
y[[layer_month_i]]

# Pd: Try changed brick by stack in case of problems (y <- stack(y))