如何确定时间序列数据的均值?

时间:2014-02-03 13:26:32

标签: r time-series

我有每2秒的样本测量数据,我想确定每2分钟这个时间序列的平均值和标准差。任何帮助将不胜感激。

      Date     Times  Pos       Date and time pressure temp
01.01.2013  02:20:01    A   2013-01-01 02:20:25.335 140.741 
01.01.2013  02:20:02    A   2013-01-01 02:20:26.091 140.741
1.01.2013   02:20:03    A   2013-01-01 02:20:26.091 140.741

1 个答案:

答案 0 :(得分:1)

# example data
set.seed(1)
df <- data.frame(dates = sort(Sys.time() + sample(1:1000, size=100)), 
                 values = rnorm(100, 100, 50))

# 2 minute groups
df$groups <- cut.POSIXt(df$dates, breaks="2 min")

# summary
require(plyr)
ddply(df, "groups", summarise, mean=mean(values), sd=sd(values))
#                groups      mean       sd
# 1 2014-02-03 14:35:00 114.60027 55.67169
# 2 2014-02-03 14:37:00 107.16711 57.97990
# 3 2014-02-03 14:39:00  99.36876 45.03428
# 4 2014-02-03 14:41:00 111.37508 44.37829
# 5 2014-02-03 14:43:00  93.33474 46.33670
# 6 2014-02-03 14:45:00 108.71795 40.43259
# 7 2014-02-03 14:47:00  85.60400 29.38563
# 8 2014-02-03 14:49:00  83.57215 69.01886
# 9 2014-02-03 14:51:00  26.82735 12.52657

修改

关于您的示例数据:

df <- read.table(sep=";", header=TRUE, stringsAsFactors=FALSE, text="
Date;Times;Pos;Date and time;pressure;temp
01.01.2013;02:20:01;A;2013-01-01 02:20:25;.335;140.741
01.01.2013;02:20:02;A;2013-01-01 02:20:26;.091;140.741
1.01.2013;02:20:03;A;2013-01-01 02:20:26;.091;140.741")
df$dates <- as.POSIXct(paste(df$Date, df$Times), 
                       format="%d.%m.%Y %H:%M:%S")
df$groups <- cut.POSIXt(df$dates, breaks="2 sec")
require(plyr)
ddply(df, "groups", summarise, 
      mean_pressure=mean(pressure), sd_pressure=sd(pressure),
      mean_temp=mean(temp), sd_temp=sd(temp))
#                groups mean_pressure sd_pressure mean_temp sd_temp
# 1 2013-01-01 02:20:01         0.213   0.1725341   140.741       0
# 2 2013-01-01 02:20:03         0.091          NA   140.741      NA