将数据帧转换为R中的时间序列

时间:2017-07-09 13:08:41

标签: r statistics time-series

我有过去两年半的月度数据。我想将我的数据框转换为时间序列。所以我应该

Start :: 2015-01-01

End :: 2017-06-01

Frequency : 1

我试过了

ts (df [, -1], start = df [1, 1], end = df [29, 1])

但我从中得到了这个真正有线的输出。

    Time Series:
    Start = 16436 
    End = 17287 
   Frequency = 1 

            date inflow
   1  2015-01-01   6434

   2  2015-02-01   5595

   3  2015-03-01   3101

   4  2015-04-01   3475

   5  2015-05-01   6519

   6  2015-06-01   7251

   7  2015-07-01   4200

   8  2015-08-01   3622

   9  2015-09-01   4782

   10 2015-10-01   6503

   11 2015-11-01   9460

   12 2015-12-01  15623

   13 2016-01-01  18393

   14 2016-02-01  14410

   15 2016-03-01  11210

   16 2016-04-01  10582

   17 2016-05-01  14316

   18 2016-06-01  11876

   19 2016-07-01  13676

   20 2016-08-01  12466

   21 2016-09-01  17326

   22 2016-10-01  15845

   23 2016-11-01  15569

   24 2016-12-01  24933

   25 2017-01-01  35050

   26 2017-02-01  26008

   27 2017-03-01  25767

   28 2017-04-01  17858

   29 2017-05-01  21089



dput(df)
structure(list(date = structure(c(16436, 16467, 16495, 16526, 
16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801, 
16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075, 
17106, 17136, 17167, 17198, 17226, 17257, 17287, 17318), class = "Date"), 
    inflow = c(6434L, 5595L, 3101L, 3475L, 6519L, 7251L, 4200L, 
    3622L, 4782L, 6503L, 9460L, 15623L, 18393L, 14410L, 11210L, 
    10582L, 14316L, 11876L, 13676L, 12466L, 17326L, 15845L, 15569L, 
    24933L, 35050L, 26008L, 25767L, 17858L, 21089L, 13570L)), row.names = c(NA, 
-30L), class = "data.frame", .Names = c("date", "inflow"))

非常感谢提前!!

2 个答案:

答案 0 :(得分:3)

1)动物园可能最简单的方法是将其转换为"zoo"类,并将其转换为"ts"类。 "yearmon"类是动物园包中提供的用于表示月度数据的类,与ts中的频率12数据密切对应。结果是"ts"类系列的长度与df中的行数相同。

library(zoo)
as.ts(read.zoo(df, FUN = as.yearmon))

##        Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
## 2015  6434  5595  3101  3475  6519  7251  4200  3622  4782  6503  9460 15623
## 2016 18393 14410 11210 10582 14316 11876 13676 12466 17326 15845 15569 24933
## 2017 35050 26008 25767 17858 21089 13570  

根据您的目的,您可能希望将其保留为"zoo"课程时间序列,在这种情况下省略as.ts

1a)使用zoo的另一种方法是:

ts(df$inflow, start = as.yearmon(df$date[1]), freq = 12)

2)base 这个更长但不使用任何包:

mo <- as.numeric(format(df$date[1], "%m"))
yr <- as.numeric(format(df$date[1], "%Y"))
ts(df$inflow, start = c(yr, mo), freq = 12)

如果知道系列总是在1月开始,那么我们可以省略mo的定义并写:

ts(df$inflow, start = yr, freq = 12)

注意:问题中的输入df为:

df <- 
structure(list(date = structure(c(16436, 16467, 16495, 16526, 
16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801, 
16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075, 
17106, 17136, 17167, 17198, 17226, 17257, 17287, 17318), class = "Date"), 
inflow = c(6434L, 5595L, 3101L, 3475L, 6519L, 7251L, 4200L, 
3622L, 4782L, 6503L, 9460L, 15623L, 18393L, 14410L, 11210L, 
10582L, 14316L, 11876L, 13676L, 12466L, 17326L, 15845L, 15569L, 
24933L, 35050L, 26008L, 25767L, 17858L, 21089L, 13570L)), row.names = 
c(NA, 30L), class = "data.frame", .Names = c("date", "inflow"))

答案 1 :(得分:1)

你的29应该是30,因为你实际上是在切断最后一次约会。不要对最后一行编号进行硬编码,最好用以下函数替换它:

ts ( df [, -1], start = df [1, 1], end = df [nrow (df), 1] )