use*_*911 6 r raster time-series data-manipulation
在这里我发现了一个非常有趣的博客:温度效应的临界阈值和经验方法非常有趣,所以我想在R中实现它的想法.但是,我有德国历史日常温度(15年历史)的多层栅格数据大RasterBrick对象的日平均温度.根据在灵感的帖子中讨论的经验方法,我需要从我的多层栅格数据构建温度分布.
更新2:可重现的shapefile:
我知道从第三方网站下载shapefile是不实际的,所以在这里我想出了可重复的shapefile来尝试:
library(sf)
library(maps)
library(rgeos)
library(mapdata)
germany <- st_as_sf(map("Germany", plot = FALSE, fill = TRUE))
write_sf(germany, "germany.shp")
Run Code Online (Sandbox Code Playgroud)
为了便于跟进我的帖子,我创建了可重现的栅格数据以便在R中使用.我还提供了来自欧盟统计局网站的德国shapefile; 这里是动态的shapefile(我可以保证链接非常安全,文件非常小):eurostat'formfile,这里是方便的可重现的栅格数据:
可重复的数据
library(raster)
library(lubridate)
library(tidyverse)
r <- raster(xmn=5.75, xmx= 15, ymn = 47.25, ymx =55,res=c(0.25,0.25))
Deu_crop <- do.call(stack,lapply(1:5479,function(i) setValues(r,round(runif(n = ncell(r),min = -10,max = 25)))))
names(Deu_crop) <- paste0('X',gsub('-','.',ymd('1980.01.01') + days(1:5479)))
shp <- shapefile('eurostat_NUTS3/deu_adm_2006.shp')
e <- raster::extract(Deu_crop,shp)
names(e) <- shp$NUTS_ID
Run Code Online (Sandbox Code Playgroud)
因此,为了测试灵感帖子中提供的工作流程,我需要设计几个全局变量,这些变量将用于此处介绍的辅助函数.但我不明白如何设计一些用于完成其工作流程的关键全局变量; 建议定义全局变量,如:w- 天气数据; tempDat:特定的汇总天气数据; Trows:span聚合网格数据; 和T:整数温度矢量(详细信息可在此处找到:详细信息).
我想估算网格化的每日天气数据随时间的温度分布.但是我很难测试这个灵感的帖子中提出的经验步骤,因为它没有提到处理多层栅格数据的解决方案,所以我不知道如何自己采用它的奇妙想法在R.
这是我在灵感帖子中使用helper funcion之前从shapefile(eurostat'formfile)聚合多个多边形栅格数据的方法:
初始尝试操纵多层栅格:
rasterHelper <- function(ix,e){
gather(data.frame(e[[ix]],stringsAsFactors = F),'colname','temp') %>%
group_by(colname) %>% summarise(temp = mean(temp)) %>% ungroup() %>% # spatial mean
mutate(year = sub('X(\\d{4}).+','\\1',colname)) %>%
group_by(year) %>% summarise_all(funs(sum)) %>% mutate(NUTS_ID = names(e)[ix])
}
do.call(rbind,lapply(1:length(e),function(ix) rasterHelper(ix)))
Run Code Online (Sandbox Code Playgroud)
但我的上述尝试没有奏效; 在我的尝试中,我打算聚合每个多边形的温度栅格数据.灵感帖子的实现非常有用,但仍然难以跟进处理多层栅格数据.我假设我应该在每个栅格图层上工作并随着时间的推移构建温度分布,但是真的没有一个坚实的想法如何在R中做任何想法?
更新:
以下是我从中得到灵感的论文:非线性温度效应......,但实施所提出的方法对我来说仍然具有挑战性,即使我遵循相应博客中提出的工作流程:在温度效应中搜索临界阈值
是否有人指出如何对R中的多层栅格数据采用其经验方法?如何估算温度随时间的分布?我怎样才能在R中实现这一点?还有什么想法?谢谢
我不太确定你想做什么。
设置一个较小的示例:
library(raster)
lux <- shapefile(system.file("external/lux.shp", package="raster"))
r <- raster(lux)
s <- stack(lapply(1:12, function(i) setValues(r, 1:ncell(r))))
e <- extract(s, lux)
Run Code Online (Sandbox Code Playgroud)
现在你说你想要聚合——这有点含糊,但也许你想要的是
x <- lapply(e, function(i) apply(i,2,mean))
Run Code Online (Sandbox Code Playgroud)
相当于
y <- extract(s, lux, fun='mean')
Run Code Online (Sandbox Code Playgroud)