使用purrr :: map重新组织列表中rasterstacks中的图层

时间:2018-03-10 15:21:45

标签: r tidyverse purrr

我需要在列表中重新排列一些rasterStacks / rasterBricks,我想使用purrr来解决它(以学习并保持我的代码在该样式中)。

我的输入列表在每个元素中都有rasterBricks,每个rasterBrick都有几个层:

> 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)

我想使用purrr来获取一个新列表,其中第一个元素是rasterBrick(或堆栈),其中所有层都名为bio_1(每个rasterBrick中的第一层),第二个包含所有bio_2 ...

的堆栈

我需要这个以后用它们的值和scale()制作一个矩阵。所以,这样的东西适用于所提到的一层:

clim_diff %>%
  map(~ .x[["bio_1"]]) %>% 
  map_dfc(~ values(.x)) %>% t %>% 
  scale

有没有办法让purrr为我的所有变量做到这一点?感谢!!!

编辑 - clim_diff可以使用以下代码重现:

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 个答案:

答案 0 :(得分:2)

我会创建一个名为arrange_ras的函数,然后使用purrr::map将其应用到您的bio_xxx名称列表

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 ...