简体   繁体   English

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

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

I am wondering if I can use the ts() functions to analyse some data where the time points are not dates.我想知道是否可以使用 ts() 函数来分析一些时间点不是日期的数据。

My vector looks like this.我的矢量看起来像这样。

   0    3    5    8   12 
20.0 14.4 80.0 20.0  4.0 

I would like to convert this into a Time-Series object to make use of the ts() functions but am struggling to.我想将其转换为时间序列 object 以使用 ts() 函数,但我正在努力。 I think the ts() function assumes dates as an input and my data does not have this.我认为 ts() function 假设日期作为输入,而我的数据没有这个。

Is there a method I can use to make my data look like the output from the following functions?有没有一种方法可以通过以下函数使我的数据看起来像 output?

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 

We can create我们可以创造

  • a zoo series (z).动物园系列 (z)。 zoo generalizes ts allowing arbitrary unique times. zoo 泛化 ts 允许任意唯一时间。
  • a ts series with NAs and times 0, 1, 2, 3, ..., 12 or具有 NA 和时间 0、1、2、3、...、12 或
  • a ts series ignoring the times and using 1, 2, 3, 4, 5 for the times一个 ts 系列忽略时代并使用 1, 2, 3, 4, 5 作为时代

with the following code:使用以下代码:

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

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

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