结合(或替换)索引与 dplyr 过滤(绘制光栅)

Combine (or replace) indexing with dplyr filtering (to plot raster)

我有一个 data.frame/tibble 有一个变量,它是一个光栅砖列表,例如 a:

library(dplyr)
library(raster)

x <- matrix(
  data = rnorm(100, 0, 1),
  nrow = 10,
  ncol = 10
) %>%
  raster

y <- matrix(
  data = rnorm(100, 0, 1),
  nrow = 10,
  ncol = 10
) %>%
  raster

z <- matrix(
  data = rnorm(100, 0, 1),
  nrow = 10,
  ncol = 10
) %>%
  raster

a <- tribble(
  ~thing, ~map,
  "b", brick(x, y),
  "c", brick(z, y),
  "d", brick(x, z),
  "e", brick(y, z),
)

a
#> # A tibble: 4 x 2
#>   thing map       
#>   <chr> <list>    
#> 1 b     <RstrBrck>
#> 2 c     <RstrBrck>
#> 3 d     <RstrBrck>
#> 4 e     <RstrBrck>

我想从砖块中取出一些光栅来绘制,例如:

plot(a$map[[3]][[2]])

但是我不想使用行的索引,而是想使用 dplyr::filter 到 select 适当的行来绘制,我可以使用 pull 到达适当的列但是我我无法理解如何到达砖块对象中的适当光栅以绘制它,而不是将其分配为新对象然后索引新对象。我想做的是像下面这样的事情(失败了):


a %>%
  filter(thing == "d") %>%
  pull(map)[[2]] %>%
  plot
#> Error in pull(map): object 'map' not found

解决方案?

reprex package (v0.3.0)

于 2021-02-03 创建

试试这个

a %>%
  filter(thing == "d") %>%
  pull(map) %>%
  .[[1]] %>% 
  .[[2]] %>% 
  plot

a %>%
  filter(thing == "d") %>%
  .$map %>% 
  .[[1]] %>% 
  .[[2]] %>% 
  plot

请注意 pull(map) 等同于 .$map