O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required