【问题标题】:Convert vector with irregular time points (not dates) into an R time-series object将具有不规则时间点(不是日期)的向量转换为 R 时间序列对象
【发布时间】:2021-03-10 18:54:44
【问题描述】:

我想知道是否可以使用 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 

【问题讨论】:

  • ts(c("0"=20, "3"=14.4, "5"=80, "8"=20, "12"=4)) 有什么问题?
  • @jay.sf,end=5 正确吗?

标签: r time-series data-wrangling


【解决方案1】:

我们可以创造

  • 动物园系列 (z)。 zoo 泛化 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

【讨论】:

    猜你喜欢
    • 2011-04-23
    • 2014-09-02
    • 2018-02-10
    • 2020-09-21
    • 2011-02-04
    • 2017-05-22
    • 1970-01-01
    • 2011-11-17
    相关资源
    最近更新 更多