从具有月值的数组中计算三个月的平均值
Calculating means for trimesters from an array with monthly values
我有一个包含月降水值的大数组(维度=144(经度)*72(纬度)*395(时间)),其中时间从 1979-01-03 (year/month/day) 开始,并且结束于 2011-11-03。
我需要计算均值和标准差,首先是每个月(即每个月和域的每个点一个均值和一个标准差)。我是这样做的:
months<-c(as.character(1:12))
years<-c(as.character(1979:2011))
lista<-list()
for (i in months){
lista[[i]]<-which(format(time2,"%m")==i) #time2 is the time dimension from the
# array, in format: 1979-01-03, 1979-02-03,etc.
}
monthly_means<-list(); monthly_sd<-list()
for(i in 1:length(lista)){
medias_mensuales[[i]]<-apply(precipitacion[,,lista[[i]]],c(1,2),mean)
sd_mensuales[[i]]<-apply(precipitacion[,,lista[[i]]],c(1,2),sd)
}
然后(这是我遇到麻烦的地方)每个三个月的均值和标准偏差(DJF、MAM、JJA、SON)。先提取每个三个月的数据然后做一些类似于我之前做的事情是个好主意吗?
问题应该包括所有数据,但我们这次在最后的注释中为您提供了一些数据。此外,该问题不包括对所需内容的解释,因此我们假设所需内容是每个 precip[i, j, ] 的所有 1 月实例的平均值,所有 2 月实例的平均值等等,所有这些都形成了一个 3d 数组其中一维是 12。对于 sd 也是如此,对于三个月也是如此。
将 precip
的最后一个定义中的日期定义为 tt
,然后将 moy
和 toy
定义为采用 Date
向量和return 一年中的月份(从 1 到 12 的数字)和一年中的三个月季度(从 1 到 4 的数字)。这些使用 yearmon
和 yearqtr
类 将日期表示为年加分数,在 toy
的情况下添加 1/12 以将 Dec 移动到 Jan,Jan 移动到 Feb,等等,以便将三个月的季度转换为日历季度。
使用这些函数将 cyc12
定义为月份数(1 到 12)的向量,将 cyc4 定义为三个月季度数(1 到 4)的向量。它们的值可以交替表示为 rep(1:12, length = 395)
和 rep(1:4, each = 3, length = 396)[-1]
.
使用 cyc12
和 cyc4
我们使用 apply
和 tapply
如图所示。
对于前两个 apply
实例,我们得到维数为 c(12, 144, 72) 的数组,对于最后两个实例,我们得到 c(4, 144, 72)。 (如果你想要置换维度,请使用 aperm
。)
library(zoo)
tt <- seq(as.Date("1979-01-03"), as.Date("2011-11-03"), "month")
moy <- function(x) cycle(as.yearmon(x))
cyc12 <- moy(tt)
means12 <- apply(precip, 1:2, function(x) tapply(x, cyc12, mean))
sd12 <- apply(precip, 1:2, function(x) tapply(x, cyc12, sd))
toy <- function(x) cycle(as.yearqtr(as.yearmon(x) + 1/12))
cyc4 <- toy(tt)
means4 <- apply(precip, 1:2, function(x) tapply(x, cyc4, mean))
sd4 <- apply(precip, 1:2, function(x) tapply(x, cyc4, sd))
备注
为了使其可重现,假设输入数据如下
Dims <- c(144, 72, 395)
precip <- array(seq_len(prod(Dims)), Dims)
我有一个包含月降水值的大数组(维度=144(经度)*72(纬度)*395(时间)),其中时间从 1979-01-03 (year/month/day) 开始,并且结束于 2011-11-03。
我需要计算均值和标准差,首先是每个月(即每个月和域的每个点一个均值和一个标准差)。我是这样做的:
months<-c(as.character(1:12))
years<-c(as.character(1979:2011))
lista<-list()
for (i in months){
lista[[i]]<-which(format(time2,"%m")==i) #time2 is the time dimension from the
# array, in format: 1979-01-03, 1979-02-03,etc.
}
monthly_means<-list(); monthly_sd<-list()
for(i in 1:length(lista)){
medias_mensuales[[i]]<-apply(precipitacion[,,lista[[i]]],c(1,2),mean)
sd_mensuales[[i]]<-apply(precipitacion[,,lista[[i]]],c(1,2),sd)
}
然后(这是我遇到麻烦的地方)每个三个月的均值和标准偏差(DJF、MAM、JJA、SON)。先提取每个三个月的数据然后做一些类似于我之前做的事情是个好主意吗?
问题应该包括所有数据,但我们这次在最后的注释中为您提供了一些数据。此外,该问题不包括对所需内容的解释,因此我们假设所需内容是每个 precip[i, j, ] 的所有 1 月实例的平均值,所有 2 月实例的平均值等等,所有这些都形成了一个 3d 数组其中一维是 12。对于 sd 也是如此,对于三个月也是如此。
将 precip
的最后一个定义中的日期定义为 tt
,然后将 moy
和 toy
定义为采用 Date
向量和return 一年中的月份(从 1 到 12 的数字)和一年中的三个月季度(从 1 到 4 的数字)。这些使用 yearmon
和 yearqtr
类 将日期表示为年加分数,在 toy
的情况下添加 1/12 以将 Dec 移动到 Jan,Jan 移动到 Feb,等等,以便将三个月的季度转换为日历季度。
使用这些函数将 cyc12
定义为月份数(1 到 12)的向量,将 cyc4 定义为三个月季度数(1 到 4)的向量。它们的值可以交替表示为 rep(1:12, length = 395)
和 rep(1:4, each = 3, length = 396)[-1]
.
使用 cyc12
和 cyc4
我们使用 apply
和 tapply
如图所示。
对于前两个 apply
实例,我们得到维数为 c(12, 144, 72) 的数组,对于最后两个实例,我们得到 c(4, 144, 72)。 (如果你想要置换维度,请使用 aperm
。)
library(zoo)
tt <- seq(as.Date("1979-01-03"), as.Date("2011-11-03"), "month")
moy <- function(x) cycle(as.yearmon(x))
cyc12 <- moy(tt)
means12 <- apply(precip, 1:2, function(x) tapply(x, cyc12, mean))
sd12 <- apply(precip, 1:2, function(x) tapply(x, cyc12, sd))
toy <- function(x) cycle(as.yearqtr(as.yearmon(x) + 1/12))
cyc4 <- toy(tt)
means4 <- apply(precip, 1:2, function(x) tapply(x, cyc4, mean))
sd4 <- apply(precip, 1:2, function(x) tapply(x, cyc4, sd))
备注
为了使其可重现,假设输入数据如下
Dims <- c(144, 72, 395)
precip <- array(seq_len(prod(Dims)), Dims)