r - 如何在更改设置输出文件名的特定参数时将函数应用于列表
问题描述
我的问题是一般性的。您如何将以下函数(在本例中为假设的)应用于两个列表:R 中的 list_1 和 list_2。这些列表同样长并且它们是成对的。
function(argument1, argument2) # where argument one should be applied to list_1 and argument2 should be applied to list_2
一个可重现的例子可以是:
as.list(paste0("raster", seq(1:10))) -> list_1 # A made-up list of raster names. In real life, this should be a list of rasters.
as.list(paste0("file", seq(1:10), ".tif")) -> list_2 # A made-up list of file names.
library(gdalUtils)
gdal_translate(src_dataset =, dst_dataset = )
# where src_dataset argument should be applied to list_1 and dst_dataset argument should be applied to list_2
提前致谢。
解决方案
mapply
功能是我一直在寻找的。
# Define a function to be applied to the lists.
func2 <- function(x, y){
gdal_translate(src_dataset = x, dst_dataset = y)
}
# Apply the function to the lists.
mapply(FUN = func2, list_1, list_2)
推荐阅读
- powershell - 如何在导出到 CSV 文件时重命名属性?
- javascript - 有没有办法使用以前的 Chrome 版本?
- python-3.x - 使用列表更新字典时出现 ValueError
- c# - 使用 HttpContext cookie 有没有办法允许在浏览器关闭时删除它以及有一个到期时间?
- android - 如何在 AOSP 源代码中添加预构建的应用程序(系统应用程序)
- php - 跨所有相关数组键同步数组值
- docker - kind cluster - 如何查看已加载的 docker-images?
- python - 在什么情况下看起来像 python 内存泄漏的问题不是泄漏?
- python - SqlAlchemy 为 ColumnClause 分配不同的列名
- perl - 使用承认/呱呱/咯咯省略错误处理程序的最顶层调用者?