Stacking an existing RasterStack multiple times
You might like mget
here, since it takes a character
vector of object names and returns the object, so you can do this:
big.stack <- stack( mget( rep( "s" , 12 ) ) )
nlayers( big.stack )
#[1] 144
Or using replicate()
to put them in a list
if you don't like using mget()
, and then stacking the list
as a list
of rasterLayers
is valid input to stack()
...
ll <- replicate( 12 , s )
big.stack2 <- stack( ll )
identical( big.stack , big.stack2 )
#[1] TRUE
copy rasters within stack
Something like this?
# example data
r <- raster(ncol=10, nrow=10)
r[]=1:ncell(r)
x <- brick(r,r,r,r,r,r)
x <- x * 1:6
y <- list()
for (i in 1:nlayers(x)) {
r <- raster(x, i)
y <- c(y, r, r, r)
}
s <- stack(y)
Computing weights from latitude to apply to raster stack values
I think this approach should work.
library(raster)
r <- landCO2[[1]] # Extracts one layer of the raster brick to pull info from. Might not be necessary
## Create raster layer that has the latitude values for each cell. using method from <https://stackoverflow.com/questions/22848836/r-assigning-x-or-y-coordinate-to-cells-of-a-raster-to-perform-calculations>
lats <- raster(matrix(yFromCell(r, 1:length(r)), nrow = 64, byrow = TRUE), #get latitude from existing raster
xmn = -181.4062, xmx= 178.5938, ymn = -89.25846, ymx = 89.25846 , # Set the extents of the raster
crs = crs(r)) # Using projection of existing raster
weights <- cos(lats*(pi/180)) #Feed this raster into your weighting formula.
plot(weights) # to see what we're looking at
This looks reasonable with weights around 1 near the equator and falling off sharply as you approach the poles.
You can now use raster algebra to apply the weights to your RasterBrick.\
CO2weighted <- landCO2 * weights # this should be a RasterBrick with 138 layers.
Related Topics
How Does R Handle Object in Function Call
R - Data Frame - Convert to Sparse Matrix
Unexpected Behaviour with Argument Defaults
Two Y Axis in Highcharter in R
How to Show a Loading Screen When the Output Is Being Calculated in a Background Process
Calculating Prediction Accuracy of a Tree Using Rpart's Predict Method
R: Clustering Results Are Different Everytime I Run
Refer to Range of Columns by Name in R
Data.Table Join and J-Expression Unexpected Behavior
How to Control Label Color Depending on Fill Darkness of Bars
Subtract Values in One Dataframe from Another
Specify Function Parameters in Do.Call
R Cmd Check Latex Error: Fatal PDFlatex - Gui Framework Cannot Be Initialized
Error Connecting to Azure Blob Storage API from R
How to Reference Column Names That Start with a Number, in Data.Table
How to Install the Odbc Driver for Snowflake Successfully on an M1 Apple Silicon MAC