R 循环问题:提取光栅堆栈(netCDF文件)每层的平均温度
我在RasterStack循环中遇到了一点小问题。我试图提取过去10年中北卡罗来纳州每个县每小时的平均温度。我能够运行一些简单的代码,并相对轻松地为每一层获取这些信息,但当我尝试循环此过程以获取光栅堆栈/块中每个时间戳的平均值时,我运气不太好!我非常感谢您的帮助或建议R 循环问题:提取光栅堆栈(netCDF文件)每层的平均温度,r,geospatial,raster,geostatistics,R,Geospatial,Raster,Geostatistics,我在RasterStack循环中遇到了一点小问题。我试图提取过去10年中北卡罗来纳州每个县每小时的平均温度。我能够运行一些简单的代码,并相对轻松地为每一层获取这些信息,但当我尝试循环此过程以获取光栅堆栈/块中每个时间戳的平均值时,我运气不太好!我非常感谢您的帮助或建议 library(tidyverse) library(rgdal) library(sp) library(raster) library(doParallel) library(foreach) library(ncdf4) l
library(tidyverse)
library(rgdal)
library(sp)
library(raster)
library(doParallel)
library(foreach)
library(ncdf4)
librarcy(exactextractr)
# Brick the NC File
r<-stack("ERA_Temps.nc")
output <- for (i in 1:length(r)) {
wgsraster <- r[[i]]
year <- str_sub(names(wgsraster), 2, 5)
month <- str_sub(names(wgsraster), 7, 8)
day <- str_sub(names(wgsraster), 10, 11)
hour <- str_sub(names(wgsraster), 13, 14)
NC_Counties <- cbind(shp, exact_extract(wgsraster, shp, c('mean')))
output_df <- data.frame(County.Name = NC_Counties$NAME, GEOID = NC_Counties$GEOID, temp_mean = NC_Counties$mean, year, month, day, hour)
return(output_df)
}
Output <- output_df
我可以相当容易地获得我需要的单个层:
wgsraster <- r[[5]]
year <- str_sub(names(wgsraster), 2, 5)
month <- str_sub(names(wgsraster), 7, 8)
day <- str_sub(names(wgsraster), 10, 11)
hour <- str_sub(names(wgsraster), 13, 14)
NC_Counties <- cbind(shp, exact_extract(wgsraster, shp, c('mean')))
output_df <- data.frame(County.Name = NC_Counties$NAME, GEOID = NC_Counties$GEOID, temp_mean = NC_Counties$exact_extract.wgsraster..shp..c..mean...,
year, month, day, hour)
output_df
County.Name GEOID temp_mean year month day hour
1 Saluda 45081 298.3141 2018 09 14 11
2 Williamsburg 45089 300.2301 2018 09 14 11
3 Dorchester 45035 299.3716 2018 09 14 11
4 Berkeley 45015 299.7389 2018 09 14 11
5 Greenwood 45047 298.5175 2018 09 14 11
wgsraster <- r[[5]]
year <- str_sub(names(wgsraster), 2, 5)
month <- str_sub(names(wgsraster), 7, 8)
day <- str_sub(names(wgsraster), 10, 11)
hour <- str_sub(names(wgsraster), 13, 14)
NC_Counties <- cbind(shp, exact_extract(wgsraster, shp, c('mean')))
output_df <- data.frame(County.Name = NC_Counties$NAME, GEOID = NC_Counties$GEOID, temp_mean = NC_Counties$exact_extract.wgsraster..shp..c..mean...,
year, month, day, hour)
output_df
County.Name GEOID temp_mean year month day hour
1 Saluda 45081 298.3141 2018 09 14 11
2 Williamsburg 45089 300.2301 2018 09 14 11
3 Dorchester 45035 299.3716 2018 09 14 11
4 Berkeley 45015 299.7389 2018 09 14 11
5 Greenwood 45047 298.5175 2018 09 14 11
Error in .local(x, i, j, ...) : you must provide an index