Rajesh Pachai
Rajesh Pachai

Reputation: 21

Time Series Data and R object type

Working with library "fpp" and object "elecequip". See below code. Why does the data not show month and year on the export? What "type" of object is "elecequip" and how do I get the month and year for this data?

fit <- stl(elecequip, s.window=5)
plot(elecequip, col="gray", main="Electrical equipment manufacturing",
    ylab="New orders index", xlab="")
lines(fit$time.series[,2],col="red",ylab="Trend")
write.table(elecequip, "ELECT DATA.txt", sep="\t")

Output looks like this for the first 2 years of data:

"x"
"1" 79.43
"2" 75.86
"3" 86.4
"4" 72.67
"5" 74.93
"6" 83.88
"7" 79.88
"8" 62.47
"9" 85.5
"10"    83.19
"11"    84.29
"12"    89.79
"13"    78.72
"14"    77.49
"15"    89.94
"16"    81.35
"17"    78.76
"18"    89.59
"19"    83.75
"20"    69.87
"21"    91.18
"22"    89.52
"23"    91.12
"24"    92.97

Upvotes: 1

Views: 337

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226182

It's a bit of a hack, but (pulling out the necessary parts of stats:::print.ts):

library("fpp")
pp <- .preformat.ts(elecequip,TRUE)
storage.mode(pp) <- "numeric"
pp <- as.data.frame(pp)

If you want it in long format (there are ways to do this other than in Hadleyverse 2, of course):

library("tidyr")
library("dplyr")
pp %>% add_rownames("year") %>% gather(month,value,-year)

Upvotes: 1

Roman Luštrik
Roman Luštrik

Reputation: 70643

It's a time series data.

> class(elecequip)
[1] "ts"

> str(elecequip)
 Time-Series [1:191] from 1996 to 2012: 79.4 75.9 86.4 72.7 74.9 ...

There may be better ways of saving the printed output as a flat table (which I think is what you're after), but sink seems to work.

sink(file = "test.txt")
print(elecequip)
sink()

Head of the test.txt file:

        Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct    Nov    Dec
1996  79.43  75.86  86.40  72.67  74.93  83.88  79.88  62.47  85.50  83.19  84.29  89.79
1997  78.72  77.49  89.94  81.35  78.76  89.59  83.75  69.87  91.18  89.52  91.12  92.97
1998  81.97  85.26  93.09  81.19  85.74  91.24  83.56  66.45  93.45  86.03  86.91  93.42
1999  81.68  81.68  91.35  79.55  87.08  96.71  98.10  79.22 103.68 101.00  99.52 111.94
2000  95.42  98.49 116.37 101.09 104.20 114.79 107.75  96.23 123.65 116.24 117.00 128.75

Upvotes: 3

Related Questions