1
votes

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"))
1
Some sample data would be helpfulTung
You are absolutely right @Tung, I just added a few lines to reproduce the first part of the clim_diff objectJavier Fajardo

1 Answers

2
votes

I would make a function called arrange_ras then use purrr::map to apply it to the list of your bio_xxx names

arrange_ras <- function(list, var){
  res <- list %>%
    map(~ .x[[var]]) %>% 
    map_dfc(~ values(.x)) %>% t %>% 
    scale
  return(res)
}

# test for 2 names only to save time
var_list <- list("bio_1", "bio_2")

# main loop
res_all <- var_list %>% 
  purrr::set_names() %>%
  map(~ arrange_ras(clim_diff, .x))

str(res_all, max.level = 1)

List of 2
 $ bio_1: num [1:2, 1:1944000] NA NA NA NA NA NA NA NA NA NA ...
  ..- attr(*, "dimnames")=List of 2
  ..- attr(*, "scaled:center")= num [1:1944000] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
  ..- attr(*, "scaled:scale")= num [1:1944000] 0 0 0 0 0 0 0 0 0 0 ...
 $ bio_2: num [1:2, 1:1944000] NA NA NA NA NA NA NA NA NA NA ...
  ..- attr(*, "dimnames")=List of 2
  ..- attr(*, "scaled:center")= num [1:1944000] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
  ..- attr(*, "scaled:scale")= num [1:1944000] 0 0 0 0 0 0 0 0 0 0 ...