尽我所能,我无法弄清楚如何让 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 正在将整个向量传递给函数,而不是单个值!非常感谢您对如何进行这项工作的建议以及对修复工作原因的解释!
提前致谢!