使用日期找出时间序列向量中数据条目的数量

Using a date to find out the number of data entry in time series vector

我目前正在处理时间序列数据。使用时间序列函数转换数据并指示正确的起点和频率后,我想知道是否可以使用日期作为指示符来引用特定数据条目的操作。更具体地说,如果我有一个从 1994Q1 到 2007Q2 的季度系列,我想知道我是否可以使用

之类的方法访问一系列数据条目

希望以下代码有助于理解我的问题:

# Assuming that full.data is the data sample
full.data <- ts(seq(1:56),start=c(1994,1),frequency = 4)

# I would like to extract specific data entries using dates. Of course,
# the example below does not work but I wondered if something similar like this would be possible:
data.short <- data.full["1995Q1" : "2005Q2"]

非常感谢任何帮助或想法,非常感谢!

您可以使用 window() 函数,它显然是为这种情况而设计的:

data.short <- window(full.data, start = c(1995, 1), end = c(2005, 2))

有关详细信息,请参阅 ?window

转换为 xts,然后使用显示的语法。如果 xts 对象没问题,你可以省略转换回来:

library(xts)

xx <- as.xts(full.data)
as.ts(xx["1995-1/2005-2"])

给予:

   Qtr1 Qtr2 Qtr3 Qtr4
1     5    6    7    8
2     9   10   11   12
3    13   14   15   16
4    17   18   19   20
5    21   22   23   24
6    25   26   27   28
7    29   30   31   32
8    33   34   35   36
9    37   38   39   40
10   41   42   43   44
11   45