本文介绍了使用 R:如何创建带有日期的时间序列对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一年中每小时采集一系列值.是否可以创建一个保留小时和年份值的时间序列对象?

I have a series of values taken every hour over a year. Is it possible to create a time-series object that retains the hour and year values?

我的代码使用股票价格第 1 列中的值,但不使用日期:

My code uses the values in column 1 of stockprices, but does not use the date:

stockprices.ts <- ts(stockprices[,1],start=1, freq=168)

推荐答案

您没有提供数据样本,但在 SO (这里例如) 涵盖了这个问题.我将 xts 用于我的时间序列工作,尽管还有其他不错的选择.

You don't provide a sample of your data, but there are a lot of other answers on SO (here for example) covering this question. I use xts for my time series work, although there are other good choices.

假设您的数据是两列,您可能有一个通过 read.table 加载的数据框:

Assuming your data is two columns, you might have a data frame loaded via read.table:

> stockprices <- data.frame(prices=c(1.1,2.2,3.3),
               timestamps=c('2011-01-05 11:00','2011-01-05 12:00','2011-01-05 13:00'))
> stockprices
  prices       timestamps
1    1.1 2011-01-05 11:00
2    2.2 2011-01-05 12:00
3    3.3 2011-01-05 13:00

您可以这样转换为 xts 时间序列:

You can convert to xts time series thus:

> require(xts)
> stockprices.ts <- xts(stockprices$prices, order.by=as.POSIXct(stockprices$timestamps))
> stockprices.ts
                    [,1]
2011-01-05 11:00:00  1.1
2011-01-05 12:00:00  2.2
2011-01-05 13:00:00  3.3

这篇关于使用 R:如何创建带有日期的时间序列对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:43
查看更多