Converting data to time series
A time series is a sequence of data points where each data point is associated with a particular time.
For example, the adjusted close of a stock is the closing price of a stock on a particular day. The time series data is stored in an R object called a time series object and it is created by using the function ts()
in R.
The basic syntax of ts
is given here:
ts(data, start, end, frequency)
Here:
data
: It is a vector or matrix containing the data valuesstart
: It is the starting point or time of first observationend
: It is the time point of last observationfrequency
: It is the number of data points per unit time
Let us consider a vector which is given by the following code:
> StockPrice< -c(23.5,23.75,24.1,25.8,27.6,27,27.5,27.75,26,28,27,25.5) > StockPrice
Now convert it into a time series object, which can be done with the following code:
> StockPricets<- ts(StockPrice,start = c(2016,1),frequency = 12) > StockPricets
The output is as follows...