我对提取R中的WorldClim数据有疑问,并获得了很好的帮助(Extract WORLDCLIM data using R for a single country)
该解决方案效果很好,但我现在面临另一个问题。
我执行以下代码以.cvs格式提取并保存WorldClim每月温度的最低温度数据
library(sp)
library(rgeos)
library(rgdal)
r<- getData('CMIP5', var='tmin', res=10, rcp=45, model='HE', year=70)
r <- r[[c(1,12)]]
poly <- shapefile("C:/Users/pc5/Desktop/r1//2011_Dist.shp")
plot(poly)
pr <- extract(r, poly, fun='mean', na.rm=TRUE, df=TRUE, weights = TRUE)
write.csv(cbind(poly$DISTRICT,ex),"Worldclim.csv", row.names = F)
但是,当我提取并保存数据时,我只得到三个变量:DISTRICT名称,第1个月的TMIN和第12个月的TMIN。我在运行代码时看到
ex <- extract(r, poly, fun='mean', na.rm=TRUE, df=TRUE, weights = TRUE)
它仅提取3个变量,而不是13(12个月+我的地区名称),尽管我可以看到所有12个月的数据都以Geotiff格式下载到默认文件夹中。我在做什么错?
r <- r[[c(1,12)]]
的情况下运行代码,您将得到所需的结果。 r <- r[[c(1,12)]]
部分代码仅从12个月数据的栅格堆栈中退出1个月和12个月。您可以通过以下代码确定这一点r<- getData('CMIP5', var='tmin', res=10, rcp=45, model='HE', year=70)
r #will show you dimensions : 900, 2160, 1944000, 12 (nrow, ncol, ncell, nlayers)
r1 <- r[[c(1,12)]]
r1 #will show you dimensions : 900, 2160, 1944000, 2 (nrow, ncol, ncell, nlayers)
plot(r) #Plot of all the 12 months
plot(r1) #Plot of only 1 and 12 month
我也编辑了以前的答案。