因此,我正在開發一個項目,該項目需要我使用空氣質量數據爲30個以上的單獨天/文件創建多個圖。R(循環)數據幀中的錯誤,替換有x行,數據有y行
我從已經完成它的人那裏學習和複製,但不知何故,我在將其與我擁有的數據集成時遇到了問題,任何幫助都是值得讚賞的。
下面是代碼:
#---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+輸出I得到1個輸出(由於某種原因!?),然後我得到這在上面作爲輸出給出的誤差:/ 。如果我沒有輸出,我會理解,但給予第一個數據文件和第二個數據文件幾乎沒有差異格式我不應該有任何格式錯誤...
夫婦更多的信息我認爲這可能有助於解決問題分別是:
- 我的數據文件看起來像這樣:
「ID」, 「海拔」 「經度」, 「緯度」, 「價值」
阿菲1027 30.54277778 38.75166667 108.2903226
艾登54 27.83666667 37.84027778 122.7096774
。
。
。
- 我複製的數據從具有數據如以下的人:
「ID」,「緯度」,「經度」,「海拔。」,「值」
2 50.69205 15.72876 816 37
3 49.735 16.0336 737 19
。
。
。
謝謝你的時間。