Reputation: 354
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2015 75 74 60 57 67 131 299 138 329 333 307 213
2016 141 174 72 219 139 163 160 82 150 177 138 138
Above is data that I am working on. I have imported data to R, Initially this is getting imported as data.frame
.
Importing the data from xlsx
file using xlsx
library
class(test)
"data.frame"
I want the class to be converted to ts
. I have tried few of the existing method but I am still not able get it done.
Upvotes: 2
Views: 5523
Reputation: 32538
You can do
test = ts(unlist(df[,-1]), start = df[1,1], frequency = ncol(df[,-1]))
test
# Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#2015 75 141 74 174 60 72 57 219 67 139 131 163
#2016 299 160 138 82 329 150 333 177 307 138 213 138
class(test)
#[1] "ts"
#Obtain the frequency
unique(cycle(test))
#[1] 1 2 3 4 5 6 7 8 9 10 11 12
#Obtain the years
unique(floor(time(test)))
#[1] 2015 2016
Upvotes: 2
Reputation: 37879
Something like this seems to work:
ts(df[-1], frequency = 1, start = df[1, 1], end = df[2, 1])
Output:
Time Series:
Start = 2015
End = 2016
Frequency = 1
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2015 75 74 60 57 67 131 299 138 329 333 307 213
2016 141 174 72 219 139 163 160 82 150 177 138 138
Upvotes: 4