-1

mydatasetI have this data set which consists of two attributes i.e Year(2016,2017,2018) and Month(JAN TO DEC). The data set contains the average sales value for all the months for the years 2016, 2017 & 2018. Now when I import this data set, it shows that the data set is a "data.frame" . However I want it to be in "ts" . Then I ran this command

data.ts<- as.ts(myData)

to convert my data into "ts". The result is as follows:

 class(data.ts)
 [1] "mts"    "ts"     "matrix"

Now, I want my data set to be in "ts" only, meaning when I run the command class(data.ts). It should show "ts" only. How can I convert my data in "ts" only? And does this "mts" and "matrix" matters or not? Also, when I plot my data using the command

 plot(data.ts)

It shows a plot in which Time is on x-axis while Year and Sales are on y-axis. On the other hand, I want to plot a graph which shows the Year in x axis and Sales values of Months on y-axis.

How do I arrange my data such that when I import the dataset, it is already in ts? Or is there any other way to do it? Also, how to arrange the dataset that it shows the Year on x axis by default. I'm really confused as all the videos that I have seen on YouTube has their data already in "ts". Also, their plot shows Year on x-axis. Hope I have made myself clear. Any help would be appreciated. How can I plot the graph such that Year is on x axis?

Unknown
  • 145
  • 2
  • 11
  • 1
    Try this: https://stackoverflow.com/questions/29046311/how-to-convert-data-frame-into-time-series – blacktj Apr 27 '19 at 17:19
  • 2
    Possible duplicate of [How to convert data frame into time series?](https://stackoverflow.com/questions/29046311/how-to-convert-data-frame-into-time-series) – jay.sf Apr 27 '19 at 17:30

1 Answers1

0

Reorder the data in a single variable:

data=as.matrix(data)
data= as.data.frame(t(data))
names(data)=c('x2016','x2017','x2018')
series=c(data$x2016,data$x2017,data$x2018)

Then take just index accordingly to the start point and the frequency of data. In your case looks like monthly from 2016 hence:

data.ts=ts(series ,start=c(2016,1), frequency=12)
plot(data.ts)
Diegolog
  • 308
  • 1
  • 7
  • When I plot my data, it shows Year & Sales on y-axis while I want Year on x- axis. How can I do that? – Unknown Apr 29 '19 at 15:25
  • You should not include the year or month variables in the data set, just the time series variable. hence if your data set is: `data=cbind.data.frame(year, month, average)` just select the average variable to put into `ts(data$average ,start=c(2016,1), frequency=12)` – Diegolog Apr 29 '19 at 15:38
  • Sorry. I did not clearly get it. Are you saying that I should just import a file with Sales Column only? – Unknown Apr 29 '19 at 15:46
  • I have edited my question which shows the kind of data set I have. The Years are 2016.2017 & 2018. While Months are from Jan to Dec which shows the average sales value below for each month. How should I import this data set? – Unknown Apr 29 '19 at 15:51
  • Do you want a line for each year? or do you want a line for the whole period? – Diegolog Apr 29 '19 at 16:01
  • A line for whole period such that the plot shows the years 2016.2017 & 2018 on the x axis and the Sales on y axis which shows whether the sales are increasing or decreasing. I hope I have made myself clear. Sorry I am new into this, – Unknown Apr 29 '19 at 16:04
  • I changed the answer so that fits your data. Please upvote the answer and mark it as correct if you are satisfied. – Diegolog Apr 29 '19 at 17:09
  • HI. Thanks. I will try this and let you know if it works – Unknown Apr 30 '19 at 17:07