I need to rearrange some rasterStacks/rasterBricks in a list, and I would like to use purrr
to work it out (to learn and keep my code within that style).
My input list has rasterBricks in each element, and each rasterBrick has a few layers:
> clim_diff[1:5]
$access1_0.year2070.rcp85
class : RasterBrick
dimensions : 111, 78, 8658, 5 (nrow, ncol, ncell, nlayers)
resolution : 0.3333333, 0.3333333 (x, y)
extent : -82, -56, -24, 13 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
data source : in memory
names : bio_1, bio_2, bio_4, bio_12, bio_13
min values : -26.07407, -29.01235, -465.18519, -1956.66667, -260.75926
max values : 31.00000, 16.38889, 484.37963, 821.46914, 181.79630
$bcc_csm1_1.year2070.rcp85
class : RasterBrick
dimensions : 111, 78, 8658, 5 (nrow, ncol, ncell, nlayers)
resolution : 0.3333333, 0.3333333 (x, y)
extent : -82, -56, -24, 13 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : in memory
names : bio_1, bio_2, bio_4, bio_12, bio_13
min values : -10.370370, -9.407407, -545.851852, -506.972222, -156.398148
max values : 4.046296, 5.901235, 169.981481, 1326.583333, 254.638889
(...)(more rasterBricks later)
I would like to use purrr
to get a new list in return where the first element is a rasterBrick (or stack) with all layers named bio_1
(the first layer in each rasterBrick), the second a stack with all bio_2
...
I need this to later make a matrix with their values and scale()
them. So, something like this works for the one layer mentioned:
clim_diff %>%
map(~ .x[["bio_1"]]) %>%
map_dfc(~ values(.x)) %>% t %>%
scale
Is there a way to properly get purrr
to do that for all my variables? Thanks!!!
EDIT - clim_diff
can be reproduced using this code:
library(dismo)
clim_diff <- list()
clim_diff$access1_0.year2070.rcp85 <- getData('CMIP5', var = "bio", res = 10, rcp = 85, year = 70, model = "AC") %>%
setNames(paste0("bio_", 1:19)) %>%
subset(c("bio_1", "bio_2", "bio_4", "bio_12", "bio_13"))
clim_diff$bcc_csm1_1.year2070.rcp85 <- getData('CMIP5', var = "bio", res = 10, rcp = 85, year = 70, model = "BC") %>%
setNames(paste0("bio_", 1:19)) %>%
subset(c("bio_1", "bio_2", "bio_4", "bio_12", "bio_13"))
clim_diff
object – Javier Fajardo