简体   繁体   English

按行和列上的时间序列处理数据

[英]Manipulating data with time series on row and column

I am new to R, and am trying to figure out how to manipulate data that has dates along rows and columns. 我是R的新手,正在尝试弄清楚如何处理行和列中具有日期的数据。

Basically, I have a data object for a date, let's say 2014-10-31. 基本上,我有一个日期的数据对象,比方说2014-10-31。 This object contains futures settlement prices for a couple months going forward: 该对象包含未来几个月的期货结算价格:

Date        2014-10-31
2014-11-01       90.54
2014-12-01       90.32
2015-01-01       90.68
2015-02-01       91.48
2015-03-01       91.03

This would exist for every weekday in a year. 一年中的每个工作日都将存在。 Now, I want something similar to this: 现在,我想要类似的东西:

Date        2014-10-28   2014-10-29   2014-10-30   2014-10-31  ... 
2014-11-01       90.54        90.53        90.57        90.54  ...
2014-12-01       90.21        90.23        90.75        90.32  ...
2015-01-01       90.70        90.45        90.65        90.68  ...
2015-02-01       91.48        91.56        91.53        91.48  ...
2015-03-01       91.02        91.03        91.01        91.03  ...

Currently, in RI have it as a list, with each day as a zoo object, with the settlement date as the name. 当前,在RI中将其作为列表,每天作为一个动物园对象,并将结算日期作为名称。 I know there must be better ways to handle this. 我知道必须有更好的方法来解决这个问题。 Are data frames the better way to go, or are there any packages that can make this easier? 数据帧是更好的方法吗?或者是否有任何软件包可以使此操作更容易?

My end goal is to be able to, for example, find the average November prices in October. 我的最终目标是,例如,能够找到10月的11月平均价格。 So I need to get all columns that are in October, and average all the prices of November. 因此,我需要获取十月份的所有列,并对十一月份的所有价格进行平均。 Any help would be appreciated. 任何帮助,将不胜感激。

If you have a list of zoo objects like this: 如果您有这样的动物园对象列表:

L <- structure(list(X2014.10.28 = structure(c(90.54, 90.21, 90.7, 
91.48, 91.02), index = structure(c(16375, 16405, 16436, 16467, 
16495), class = "Date"), class = "zoo"), X2014.10.29 = structure(c(90.53, 
90.23, 90.45, 91.56, 91.03), index = structure(c(16375, 16405, 
16436, 16467, 16495), class = "Date"), class = "zoo"), 
X2014.10.30 = structure(c(90.57, 
90.75, 90.65, 91.53, 91.01), index = structure(c(16375, 16405, 
16436, 16467, 16495), class = "Date"), class = "zoo"), 
X2014.10.31 = structure(c(90.54, 
90.32, 90.68, 91.48, 91.03), index = structure(c(16375, 16405, 
16436, 16467, 16495), class = "Date"), class = "zoo")), .Names = c("X2014.10.28", 
"X2014.10.29", "X2014.10.30", "X2014.10.31"))

Then use cbind to merge them together into a single zoo object: 然后使用cbind将它们合并到一个动物园对象中:

z <- do.call(cbind, L)

giving: 给予:

> class(z)
[1] "zoo"
> z
           X2014.10.28 X2014.10.29 X2014.10.30 X2014.10.31
2014-11-01       90.54       90.53       90.57       90.54
2014-12-01       90.21       90.23       90.75       90.32
2015-01-01       90.70       90.45       90.65       90.68
2015-02-01       91.48       91.56       91.53       91.48
2015-03-01       91.02       91.03       91.01       91.03

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

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