0

尽我所能,我无法弄清楚如何让 plyr 工作。对于这个特定示例的任何帮助,以及任何解释您的示例为何有效的奖励积分,将不胜感激。

数据在这里,以防有人想让这个例子“可行”

设置:

library(ncdf)
library(plyr)
longs = seq.int(from=1,to=4320,by=6)
lats = seq.int(from=1,to=2160,by=6)
blocksize = 6
maize.nc = open.ncdf('maize_5min.nc')

此 for 循环有效,但速度慢且不并行:

latlongcoords = NULL
for (x in 1:length(longs))  {
for (y in 1:length(lats))   {   
    lat = mean(get.var.ncdf(maize.nc,varid="latitude",start = lats[x],count = blocksize))
    lon = mean(get.var.ncdf(maize.nc,varid="longitude",start = longs[y],count = blocksize))
    rw = c(lat,lon)
    latlongcoords = rbind(latlongcoords,rw)
    }
    #print(x)
    }
save(latlongcoords,file="latlongcoords")

我想做这样的事情:

require(doMC)
registerDoMC(32)
x = seq(1:10)  #shorten it for testing purposes
y = seq(1:10)  #shorten it for testing purposes
makecoords = function(x,y){
    lat = mean(get.var.ncdf(maize.nc,varid="latitude",start = lats[x],count = blocksize))
    lon = mean(get.var.ncdf(maize.nc,varid="longitude",start = longs[y],count = blocksize))
    c(lat,lon)
    }
latlongcoords = NULL
latlongcoords = aaply(.data = cbind(x,y), .margins=2, .fun=makecoords(x,y),.parallel=TRUE)

当我运行它时,我收到以下错误消息:

Error in get.var.ncdf(maize.nc, varid = "latitude", start = lats[x], count = blocksize) : 
  Error: variable has 1 dims, but start has 10 entries.  They must match!

看起来 plyr 正在将整个向量传递给函数,而不是单个值!非常感谢您对如何进行这项工作的建议以及对修复工作原因的解释!

提前致谢!

4

1 回答 1

1

尝试这个。我认为你必须通过.margin=1而不是在2这里。也就是说,如果您想按 row传递值,那么.margin = 1. x[1]y[1]首先传递给 w。因此,我们将使用w[1]and访问它们w[2]并将其传递给您的makecoords函数。我希望这是你所期望的。如果没有,请随时在评论下写下出了什么问题。

latlongcoords <- aaply(cbind(x,y), 1, function(w) 
                 makecoords(w[1], w[2]), .parallel=TRUE)

它适用.parallel = FALSE于我。现在不能测试.parallel=TRUE

head(latlongcoords)
# X1              1            2
#   1    89.7500025 -179.7499949
#   2    86.7500025 -176.7499949
#   3    83.7500025 -173.7499949
#   4    80.7500025 -170.7499949
#   5    77.7500025 -167.7499949
#   6    74.7500025 -164.7499949
于 2013-03-28T20:02:04.143 回答