How to merge two multiple multi-layers raster data into one in R?

Here I have 16 years daily minimum and temperature data observation in two different `RasterBrick` object. I want to aggregate/merge these multi-layers rasters (daily minimum and daily maximum) into one where both daily minimum and daily maximum temperature can be easily manipulated in each raster layer for further use. I don't have a solid idea to make this aggregation easily in R.

Here is how original multi-layer raster looks like:

``````> temp_1995_2010
class       : RasterStack
dimensions  : 201, 464, 93264, 5844  (nrow, ncol, ncell, nlayers)
resolution  : 0.25, 0.25  (x, y)
extent      : -40.5, 75.5, 25.25, 75.5  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84
names       : X1995.01.01, X1995.01.02, X1995.01.03, X1995.01.04, X1995.01.05, X1995.01.06, X1995.01.07, X1995.01.08, X1995.01.09, X1995.01.10, X1995.01.11, X1995.01.12, X1995.01.13, X1995.01.14, X1995.01.15, ...
``````

Here is the reproducible example for 16 years' daily minimum and daily maximum temperature observation:

``````library(raster)
library(lubridate)
library(tidyverse)
``````

16 years' daily maximum temperature data:

``````T_max <- raster(xmn=5.75, xmx= 15, ymn = 47.25, ymx =55,res=c(0.25,0.25))
Deu_df_max <- do.call(stack,lapply(1:5844,function(i) setValues(T_max,round(runif(n = ncell(T_max),min = 2,max = 35)))))
names(Deu_df_max ) <- paste0('X',gsub('-','.',ymd('1980.01.01') + days(1:5844)))
``````

16 years' daily minimum temperature data:

``````T_min <- raster(xmn=5.75, xmx= 15, ymn = 47.25, ymx =55,res=c(0.25,0.25))
Deu_df_min <- do.call(stack,lapply(1:5844,function(i) setValues(T_min ,round(runif(n = ncell(T_min ),min = -5,max = 24)))))
names(Deu_df_min ) <- paste0('X',gsub('-','.',ymd('1980.01.01') + days(1:5844)))
``````

desired output:

so in my desired output, the structure of each raster later will be like this (just an fake example here):

``````date       year month day FID      county state  lat   long    tmax   tmin
1 1980-1-1 1980    1  1 6055 Napa County    CA 38.5 -122.5 12.5000 4.1100
2 1980-1-2 1980    1  2 6055 Napa County    CA 38.5 -122.5 12.5445 4.2894
3 1980-1-3 1980    1  3 6055 Napa County    CA 38.5 -122.5 12.5878 4.4574
4 1980-1-4 1980    1  4 6055 Napa County    CA 38.5 -122.5 12.6298 4.6144
5 1980-1-5 1980    1  5 6055 Napa County    CA 38.5 -122.5 12.6706 4.7604
6 1980-1-6 1980    1  6 6055 Napa County    CA 38.5 -122.5 12.7103 4.8959
``````

update:

perhaps, the skeleton of my desired output is not correct, but the point is I want daily minimum and daily maximum temperature in one common multi-layer raster data. Is that doable?

I want to keep structures of original raster later but put daily minimum and maximum temperature value into one common multi-layer raster. How can I make this happen? Is there any efficient way to get this done? Any instant help to merge two multi-layer rasters into one? Any idea?

• stack(T_max, T_min) should give you a single stack, containing first the t max and then the t min layers – lbusett Jul 3 '18 at 18:25