将具有不规则时间点(不是日期)的向量转换为 R 时间序列对象

Convert vector with irregular time points (not dates) into an R time-series object

我想知道是否可以使用 ts() 函数来分析一些时间点不是日期的数据。

我的矢量看起来像这样。

   0    3    5    8   12 
20.0 14.4 80.0 20.0  4.0 

我想将其转换为时间序列对象以利用 ts() 函数,但我很难做到。我认为 ts() 函数假定日期作为输入,而我的数据没有这个。

有没有一种方法可以使我的数据看起来像以下函数的输出?

library(stats)
suns <- ts.intersect(lynx, sunspot.year)[, "sunspot.year"]
suns


Time Series:
Start = 1821 
End = 1934 
Frequency = 1 
  [1]   6.6   4.0   1.8   8.5  16.6  36.3 

我们可以创造

  • 动物园系列 (z)。动物园概括了允许任意唯一时间的 ts。
  • 具有 NA 和时间 0、1、2、3、...、12 或
  • 的 ts 系列
  • 一个ts系列忽略时间,使用1,2,3,4,5作为时间

使用以下代码:

library(zoo)
values <- c(20, 14.4, 80, 20, 4)
tt <- c(0, 3, 5, 8, 12)
z <- zoo(values, tt)
z
##    0    3    5    8   12 
## 20.0 14.4 80.0 20.0  4.0 

as.ts(z)  # fill with NAs
## Time Series:
## Start = 0 
## End = 12 
## Frequency = 1 
##  [1] 20.0   NA   NA 14.4   NA 80.0   NA   NA 20.0   NA   NA   NA  4.0

ts(values) # ignores times and uses 1:5 instead
## Time Series:
## Start = 1 
## End = 5 
## Frequency = 1 
## [1] 20.0 14.4 80.0 20.0  4.0