使用r中的栅格包聚合季节性方法(Aggregating seasonal means with the raster package in r)

编程入门 行业动态 更新时间:2024-10-10 11:29:14
使用r中的栅格包聚合季节性方法(Aggregating seasonal means with the raster package in r)

我试图将每日数据(35年)汇总到每月,然后使用R中的光栅包计算季节性平均值(我知道如何使用CDO)。 下面是我的代码,它为所有年份(140层)输出4种季节性手段。 如何循环输出仅4层(4季)? 我感谢您的帮助。

dailydata <- brick ("dailyrain.nc") dates <- seq(as.Date("1981-01-01"), as.Date("2015-12-31"), by="day") months <- format(dates, "%Y-%m") Aggregate2Monthly <- function(x) { agg <- aggregate(x, by=list(months), sum) return(agg$x) } mothlydata <- calc(dailydata, Aggregate2Monthly) mondates <- seq(as.Date("1981-01-01"), as.Date("2015-12-31"), by="month") years <- format(mondates, "%Y") seasons.def=c(1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4) years.seasons <- paste(years, seasons.def, sep="-") nyears <- years[!duplicated(years)] nseas <- seasons.def[!duplicated(seasons.def)] Aggregate2Seasons <- function(x) { agg <- aggregate(x, by=list(years.seasons), mean) return(agg$x) } seasonsdata <- calc(mothlydata, Aggregate2Seasons)

I am attempting to aggregate daily data (35 years) to monthly then calculate seasonal mean using the raster package in R (I know how to do it with CDO). Below is my code, which outputs 4 seasonal means for all years (140 layers). How can I loop to output only 4 layers ( for the 4 seasons)?. I appreciate your help.

dailydata <- brick ("dailyrain.nc") dates <- seq(as.Date("1981-01-01"), as.Date("2015-12-31"), by="day") months <- format(dates, "%Y-%m") Aggregate2Monthly <- function(x) { agg <- aggregate(x, by=list(months), sum) return(agg$x) } mothlydata <- calc(dailydata, Aggregate2Monthly) mondates <- seq(as.Date("1981-01-01"), as.Date("2015-12-31"), by="month") years <- format(mondates, "%Y") seasons.def=c(1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4) years.seasons <- paste(years, seasons.def, sep="-") nyears <- years[!duplicated(years)] nseas <- seasons.def[!duplicated(seasons.def)] Aggregate2Seasons <- function(x) { agg <- aggregate(x, by=list(years.seasons), mean) return(agg$x) } seasonsdata <- calc(mothlydata, Aggregate2Seasons)

最满意答案

您希望按年和月的组合进行汇总。

months <- format(dates, "%Y-%m")

分组月份(根据您的评论):

groups <- function(x) { d <- as.POSIXlt(x) ans <- character(length(x)) ans[d$mon %in% 0:1] <- "JF" ans[d$mon %in% 2:4] <- "MAM" ans[d$mon %in% 5:8] <- "JJAS" ans[d$mon %in% 9:11] <- "OND" ans }

现在使用groups(dates)作为分组变量。 检查:

data.frame(dates, groups(dates)) ## dates groups.dates. ## 1 1981-01-01 JF ## 2 1981-01-02 JF ## 3 1981-01-03 JF ## 4 1981-01-04 JF ## 5 1981-01-05 JF ## 6 1981-01-06 JF

You want to aggregate by a combination of year and month.

months <- format(dates, "%Y-%m")

Grouping months (as per your comment):

groups <- function(x) { d <- as.POSIXlt(x) ans <- character(length(x)) ans[d$mon %in% 0:1] <- "JF" ans[d$mon %in% 2:4] <- "MAM" ans[d$mon %in% 5:8] <- "JJAS" ans[d$mon %in% 9:11] <- "OND" ans }

Now use groups(dates) as the grouping variable. Check:

data.frame(dates, groups(dates)) ## dates groups.dates. ## 1 1981-01-01 JF ## 2 1981-01-02 JF ## 3 1981-01-03 JF ## 4 1981-01-04 JF ## 5 1981-01-05 JF ## 6 1981-01-06 JF

更多推荐

本文发布于:2023-08-08 01:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:栅格   季节性   方法   Aggregating   package

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!