将数据帧转换为 R 中的时间序列 [英] convert data frame to time series in R

查看:33
本文介绍了将数据帧转换为 R 中的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

开始 :: 2015-01-01结束 :: 2017-06-01频率 : 1

我试过了

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

但我从中得到了真正的有线输出.

 时间序列:开始 = 16436结束 = 17287频率 = 1日期流入1 2015-01-01 64342 2015-02-01 55953 2015-03-01 31014 2015-04-01 34755 2015-05-01 65196 2015-06-01 72517 2015-07-01 42008 2015-08-01 36229 2015-09-01 478210 2015-10-01 650311 2015-11-01 946012 2015-12-01 1562313 2016-01-01 1839314 2016-02-01 1441015 2016-03-01 1121016 2016-04-01 1058217 2016-05-01 1431618 2016-06-01 1187619 2016-07-01 1367620 2016-08-01 1246621 2016-09-01 1732622 2016-10-01 1584523 2016-11-01 1556924 2016-12-01 2493325 2017-01-01 3505026 2017-02-01 2600827 2017-03-01 2576728 2017-04-01 1785829 2017-05-01 21089输出(df)结构(列表(日期 = 结构(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"),流入量 = 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) 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 <- as.numeric(format(df$date[1], "%m"))yr <- as.numeric(format(df$date[1], "%Y"))ts(df$inflow, start = c(yr, mo), freq = 12)

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

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

注意:问题的输入df是:

df <-结构(列表(日期 = 结构(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"),流入量 = 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"))

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

这篇关于将数据帧转换为 R 中的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆