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

问题描述

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

library(dplyr)
library(raster)

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

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

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

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

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 选择要绘制的适当行,而不是索引行,我可以使用 pull 到达适当的列,但我无法理解围绕如何获得砖对象内的适当光栅以对其进行绘制,而不是将其分配为新对象然后索引新对象。我想要做的是如下所示(失败):


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

解决方案?

reprex package (v0.3.0) 于 2021 年 2 月 3 日创建

解决方法

试试这个

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

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

请注意,pull(map) 等价于 .$map