如何从 R 中的栅格列表中提取具有相同范围的栅格
How to extract rasters with the same extent from a list of rasters in R
我有一个由栅格组成的列表对象,其中有两种栅格的范围。如何提取具有相同范围的那些并将它们另存为 R 中的新列表?无法提供可重现的示例,但下面的代码可以帮助您理解问题。
一个假设的例子:
library(raster)
list_raster # Suppose this is a list having 48 rasters.
names(list_raster) <- paste0("raster", seq(1:48)) # Assigning names makes it possible to use the dollar sign.
lapply(list_raster, extent) # Gives 48 results but only two unique raster extents.
# I would like to know which of the rasters has == extent(list_rasters$raster1)
您可以使用 sapply
:
same_as_r1 <- sapply(list_raster, function(x) extent(x) == extent(list_raster$raster1))
并用它对您的列表进行子集化:
group1 <- list_raster[same_as_r1]
group2 <- list_raster[!same_as_r1]
我有一个由栅格组成的列表对象,其中有两种栅格的范围。如何提取具有相同范围的那些并将它们另存为 R 中的新列表?无法提供可重现的示例,但下面的代码可以帮助您理解问题。
一个假设的例子:
library(raster)
list_raster # Suppose this is a list having 48 rasters.
names(list_raster) <- paste0("raster", seq(1:48)) # Assigning names makes it possible to use the dollar sign.
lapply(list_raster, extent) # Gives 48 results but only two unique raster extents.
# I would like to know which of the rasters has == extent(list_rasters$raster1)
您可以使用 sapply
:
same_as_r1 <- sapply(list_raster, function(x) extent(x) == extent(list_raster$raster1))
并用它对您的列表进行子集化:
group1 <- list_raster[same_as_r1]
group2 <- list_raster[!same_as_r1]