How to do it...

  1. Read the data. The file has 100 rows and a single column named sales:
> s <- read.csv("ts-example.csv") 
  1. Convert the data to a simplistic time series object without any explicit notion of time:
> s.ts <- ts(s) 
> class(s.ts) 
[1] "ts" 
  1. Plot the time series:
> plot(s.ts) 
  1. Create a proper time series object with proper time points:
> s.ts.a <- ts(s, start = 2002) 
> s.ts.a 
Time Series: 
Start = 2002  
End = 2101  
Frequency = 1  
       sales 
  [1,]    51 
  [2,]    56 
  [3,]    37 
  [4,]   101 
  [5,]    66 
  (output truncated) 
> plot(s.ts.a) 
> # results show that R treated this as an annual 
> # time series with 2002 as the starting year 

The result of the preceding ...

Get R Data Analysis Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.