且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-26 20:03:55

1) zoo 可能最简单的方法是将其转换为 "zoo" 类,然后从该类转换为"ts" 类."yearmon" 类是zoo 包中提供的一个类,用于表示月度数据,与ts 中的频率12 数据紧密对应.结果是一个 "ts" 类系列,其长度与 df 中的行数相同.

图书馆(动物园)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) 使用动物园的另一种方法是:

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

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

mo 

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

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

注意:问题的输入df是:

df 

I have monthly data fro last two and half year. I want to convert my data frame to time series. So that I should have

Start :: 2015-01-01

End :: 2017-06-01

Frequency : 1

I have tried

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

But I get this really wired output from this.

    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"))

Many thanks in advance!!

1) zoo Probably the easiest is to convert it to "zoo" class and from that to "ts" class. "yearmon" class is a class provided in the zoo package for representing monthly data and closely corresponds to frequency 12 data in ts. The result is a "ts" class series having the same length as the number of rows in 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  

Depending on what you want to do you may prefer to just leave it as a "zoo" class time series in which case omit the as.ts .

1a) An alternative way to use zoo would be:

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

2) base This is longer but does not use any packages:

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)

If it were known that the series always starts in January then we could omit the definition of mo and write:

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

Note: The input df from the question is:

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"))