如何在更改设置输出文件名的特定参数时将函数应用于列表

问题描述 投票:0回答:1

我的问题是一个普遍的问题。您如何将以下功能(在这种情况下为假设功能)应用于两个列表: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

提前感谢。

r function apply lapply sapply
1个回答
0
投票

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)
© www.soinside.com 2019 - 2024. All rights reserved.