我正在尝试编写一个循环,在其中将函数应用于列表列表。我想用 gstat 包的 idw 命令插入不同国家的温度(最高平均温度/MAXMEAN)。
idw函数需要坐标(经度、纬度)和变量MAXMEAN的信息。这些组合在数据框列表“temperatures.coordinates”(如下所示:https ://imgur.com/cr3PquB )中,该列表包含 37 个国家/地区,其中每个列表再次分为 12 个月(子列表长度为 12 )。该函数还需要来自 grd.list 数据帧的信息,该数据帧是一个长度为 37 的列表,没有子列表。
应用于单个国家和单个月份,代码如下所示:
# Create a gridded structure
grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 0.1), y = seq(from = y.range[1], to = y.range[2], by = 0.1))
coordinates(grd) <- ~x + y
gridded(grd) <- TRUE
#Interpolate surface and fix the output. Apply idw model for the data
idw <- idw(formula = MAXMEAN ~ 1, locations = temperatures.coordinates, newdata = grd)
我试过这样做:
# Create a gridded structure
grd <- list()
for (i in 1:length(countrybounds)) {
grd[[i]] <- expand.grid(x = seq(from = (x.range[[i]])[1], to = (x.range[[i]])[2], by = 0.1), y = seq(from = (y.range[[i]])[1], to = (y.range[[i]])[2], by = 0.1))
coordinates(grd[[i]]) <- ~x + y
gridded(grd[[i]]) <- TRUE
grd.list <- grd
gridded(grd.list[[i]]) <- TRUE
}
#Interpolate surface and fix the output. Apply idw model for the data
idw.list <- list()
for (i in 1:length(grd.list)) {
idw.list[[i]] <- list()
for (j in 1:length(grd[[i]])) {
idw.list[[i]][[j]] <- idw(formula = MAXMEAN ~ 1, locations = (temperatures.coordinates[[i]][[j]]), newdata = grd.list[[i]])
}
}
运行循环后我收到此错误:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘idw’ for signature ‘"formula", "data.frame"’
这在之前的类似循环中起作用,我在其中创建温度坐标的列表列表:
temperatures.coordinates <- list()
for(i in 1:length(monthlymean)){
temperatures.coordinates[[i]] <- list()
for(j in 1:length(monthlymean[[i]])){
temperatures.coordinates[[i]][[j]]<-(monthlymean[[i]][[j]])[,c("LON","LAT","MAXMEAN")]
}
}
我不确定我是否提供了所有相关信息并且我已经坚持了一段时间 - 感谢任何帮助,谢谢!