简体   繁体   English

基于平均月份和滚动数据的值的差异

[英]Differences of values based on mean months and rolling data

I am trying to do something which seems simple but is proving a bit of a challenge so I hope someone can help! 我正在尝试做一些看起来很简单的事情但是证明有点挑战,所以我希望有人可以提供帮助!
I have a time series of observations of temperature: 我有一系列温度观察时间:

Lines <-"1971-01-17 298.9197
1971-01-17 298.9197
1971-02-16 299.0429
1971-03-17 299.0753
1971-04-17 299.3250
1971-05-17 299.5606
1971-06-17 299.2380
2010-07-14 298.7876
2010-08-14 298.5529
2010-09-14 298.3642
2010-10-14 297.8739
2010-11-14 297.7455
2010-12-14 297.4790"

DF <- read.table(textConnection(Lines), col.names = c("Date", "Value"))

DF$Date <- as.Date(DF$Date)

mean.ts <- aggregate(DF["Value"], format(DF["Date"], "%m"), mean)

This produces: 这会产生:

> mean.ts
  Date    Value
1   01 1.251667
2   02 1.263333

This is just an example -- my data is for many years so I can calculate a full monthly average of the data. 这只是一个例子 - 我的数据是多年的,所以我可以计算出每月的完整数据平均值。
What I then want to do is calculate the difference in for all of the January's (individually) with the mean January I have calculated above. 我当时想要做的是计算所有1月份(单独)的差异与我上面计算的1月份的平均值。

If I move away from using Date/Time class I could do this with some loops but I want to see if there is a "neat" way to do this in R? 如果我不再使用日期/时间类,我可以用一些循环来做这个但我想知道在R中是否有一个“整洁”的方法来做到这一点? Any ideas? 有任何想法吗?

You can just add the year as an aggregating variable. 您只需将年份添加为聚合变量即可。 This is easier using the formula interface: 使用公式界面更容易:

> aggregate(Value~format(Date,"%m")+format(Date,"%Y"),data=DF,mean)
   format(Date, "%m") format(Date, "%Y")    Value
1                  01               1971 298.9197
2                  02               1971 299.0429
3                  03               1971 299.0753
4                  04               1971 299.3250
5                  05               1971 299.5606
6                  06               1971 299.2380
7                  07               2010 298.7876
8                  08               2010 298.5529
9                  09               2010 298.3642
10                 10               2010 297.8739
11                 11               2010 297.7455
12                 12               2010 297.4790

At least as I understand your question you want the differences of each month with the mean of those months, so you probably you want to use ave rather than aggregate: 至少我理解你的问题,你想要每个月的差异与那些月份的平均值,所以你可能想要使用ave而不是聚合:

diff.mean.ts <- ave(DF[["Value"]], 
                        list(format(DF[["Date"]], "%m")), FUN=function(x) x-mean(x) )

If you wanted it in the same dataframe, then just assign it as a column: 如果您想在同一个数据框中使用它,那么只需将其指定为一列:

DF$ diff.mean.ts  <- diff.mean.ts 

The ave function is designed for adding columns to existing dataframes because it returns a vector of the same length as the number of values in the its first argument, in this case DF[["Value"]]. ave函数用于向现有数据帧添加列,因为它返回的长度与第一个参数中的值数相同,在本例中为DF [[“Value”]]。 In the present instance it returns all 0's which is the correct answer because there is only one value for each month. 在当前实例中,它返回所有0,这是正确的答案,因为每个月只有一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM