Reputation: 340
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!!
Upvotes: 1
Views: 17089
Reputation: 269371
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"))
Upvotes: 5
Reputation: 74
Your 29 should be a 30, since you are effectively cutting off the last date. Instead of hard coding the last row number, it is best to replace it with a function such as:
ts ( df [, -1], start = df [1, 1], end = df [nrow (df), 1] )
Upvotes: 1