因此,我正在开发一个项目,该项目需要我使用空气质量数据为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
。
。
。
谢谢你的时间。