所以我正在开展一个项目,要求我使用30多个单独日/文件的空气质量数据创建多个图。
我已经从已经完成它的人那里学习和复制了,但不知怎的,我在将它与我拥有的数据集成时遇到了问题,我们非常感谢任何帮助。
以下是代码:
#---START OF THE FOR LOOP---
all_dates = seq(begin_date, end_date, 1)
for (j in 1:length(all_dates)) {
selected_date = all_dates[j]
#read data from the file with (id, lat, lon, elev, value... enter your attributes here) columns
datapointfile = paste(variable,"_",aggregate,"_",format.Date(selected_date,"%Y-%m-%d"),".txt",sep="")
datapoints_wgs84 = read.table(datapointfile, header=TRUE)
coordinates(datapoints_wgs84) = ~lon + lat
proj4string(datapoints_wgs84) <- CRS("+init=epsg:4326")
datapoints <- spTransform(datapoints_wgs84, CRS(projection))
#INTERPOLATION STARTS HERE!
#calculate linear regression intercept and slope(observ=B+A*elev)
observlm <- lm(value ~ elev, datapoints)
datapoints$res = observlm$residuals
#calculate observ value raster using linear model (elevation / observ value)
intercept = observlm$coefficients[1]
slope = observlm$coefficients[2]
regression_grid <- intercept + srtm * slope
#interpolate the residuals using idw
idw_test = idw(res~1, datapoints, defaultgrid)
residual_grid = raster(idw_test, "var1.pred")
#add regression grid and residuals
finalgrid <- regression_grid + residual_grid
#INTERPOLATION ENDS HERE!
#START OF THE PLOT!
layout = list(vrstvastudyarea)
outfile = paste("Output", selected_date, ".png", sep="")
png(filename = outfile, width = 1500, height = 1000, pointsize = 25, bg = "white", res = 150)
nadpis = paste("Air Quality", selected_date)
print(spplot(finalgrid, at=intervaly, col.regions = grid_colors,
sp.layout=layout,
main=list(nadpis, cex=2, col="black", font=2),
colorkey=list(at=intervaly2, labels = list( at=intervaly2, cex = 1.5, labels = intervaly, lab = intervaly2), space="bottom")))
dev.off()
#END OF THE PLOT!
#---OUTPUT---
[inverse distance weighted interpolation]
Error in `[[<-.data.frame`(`*tmp*`, name, value = c(8.66783923397599, :
replacement has 14 rows, data has 18
所以基本上不是获得30+输出而是得到1输出(出于某种原因!?)然后我得到上面给出的错误作为输出:/。如果我没有输出我会理解但是给出第一个数据文件并且第二个数据文件几乎没有格式差异我不应该在格式化方面有任何错误...
我认为可能有助于解决问题的更多信息是:
“id”“elev”“lon”“lat”“value”
Afyon 1027 30.54277778 38.75166667 108.2903226
Aydin 54 27.83666667 37.84027778 122.7096774
“id”“lat”“lon”“elev”“value”
2 50.69205 15.72876 816 37
3 49.735 16.0336 737 19
感谢您的时间。
答案 0 :(得分:1)
显然,这是因为我的数据中包含一些“NA”条目。通过给它们适当的值来修正它。虽然如果我希望它能够与包含的NA值一起使用,我仍然无法解释代码是如何工作的。