本文介绍了如何使用 apply.daily/period.apply 计算 XTS 时间序列中每列的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 period.apply 函数进行高分辨率时间序列分析时遇到问题.

I have a problem using the period.apply function for my case of a high resolution time series analysis.

我想为我的数据计算统计数据(不同时期的平均值、标准差等),间隔为 10 分钟.计算每小时意味着工作正常,如此答案中所述.

I want to calculate statistics(Mean for different Periods, Stddev etc.) for my data which is in 10 min intervals. To calculate hourly means worked fine like described in this answer.

它创建一个新的 xts 对象,并为每列计算均值.如何计算每列的最大值?

It creates a new xts object with means calculated for each column. How do I calculate maximum values for each column?

这个可重现的示例描述了我的数据结构:

This reproducible example describes the structure of my data:

library(xts)
start <- as.POSIXct("2018-05-18 00:00")
tseq <- seq(from = start, length.out = 1440, by = "10 mins")
Measurings <- data.frame(
  Time = tseq,
  Temp = sample(10:37,1440, replace = TRUE, set.seed(seed = 10)),
  Variable1 = sample(1:200,1440, replace = TRUE, set.seed(seed = 187)),
  Variable2 = sample(300:800,1440, replace = TRUE, set.seed(seed = 333))
)
Measurings_xts <- xts(Measurings[,-1], Measurings$Time)
HourEnds <- endpoints(Measurings_xts, "hours")
Measurings_mean <- period.apply(Measurings_xts, HourEnds, mean)

我认为将函数参数从 mean 更改为 max 会很容易,如下所示:

I thought it would be easy to just change the function argument from mean to max, like this:

Measurings_max <- period.apply(Measurings_xts, HourEnds, max)

它提供输出,但只有一列具有整体最大值.我需要每列的每小时最大值.一个简单的解决方案将不胜感激.

It delivers output, but only one column with the overall maximum values. I need the hourly maximums of each column. A simple solution would be much appreciated.

推荐答案

mean 示例按列工作,因为有一个 zoo 方法在每一列上调用 mean(这使用方法是因为 xts 扩展了 zoo).

The mean example works by column because there's a zoo method that calls mean on each column (this method is used because xts extends zoo).

max 示例返回一个数字,因为没有 max.xtsmax.zoo 方法,因此它返回最大值整个 xts/zoo 对象.

The max example returns one number because there is no max.xts or max.zoo method, so it returns the maximum of the entire xts/zoo object.

一个简单的解决方案是定义一个辅助函数:

A simple solution is to define a helper function:

colMax <- function(x, na.rm = FALSE) {
  apply(x, 2, max, na.rm = na.rm)
}

然后在您的 period.apply 调用中使用它:

Then use that in your period.apply call:

epHours <- endpoints(Measurings_xts, "hours")
Measurings_max <- period.apply(Measurings_xts, epHours, colMax)
head(Measurings_max)
#                     Temp Variable1 Variable2
# 2018-05-18 00:50:00   29       194       787
# 2018-05-18 01:50:00   28       178       605
# 2018-05-18 02:50:00   26       188       756
# 2018-05-18 03:50:00   34       152       444
# 2018-05-18 04:50:00   33       145       724
# 2018-05-18 05:50:00   35       187       621

这篇关于如何使用 apply.daily/period.apply 计算 XTS 时间序列中每列的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:29