[R] Identifying time series

Gabor Grothendieck ggrothendieck at myway.com
Tue Oct 5 00:52:18 CEST 2004


John DeAngelis <john.deangelis <at> moorecap.com> writes:

: 
: Hello,
: 
: I am currently attempting to introduce R at my company and am trying to
: import time series data from a text file into R to graph. The format of the
: text file is in date, data (e.g.,  20040929 3.361). My problem is that I do
: not know how to get R to recognize the first column as a business date
: series. Or at the very least, I am unable to find a function that will grap
: the second column (y-axis) against the date series (x-axis). If you could
: tell me how to graph this type of data, I would greatly appreciate it. Thank
: you.


The most straight forward way is just to read it into a data frame,
convert the first column to Date class and then plot:

	x <- read.table("myseries.dat")
	colnames(x) = c("date", "value")   # label the columns
	x$date <- as.Date(as.character(x$date), "%Y%m%d")
	plot(value ~ date, x)

However, you might wish to use one of the irregular time series packages
in R.  These include zoo (in package zoo), its (in package its) and
timeSeries (in package fBasics).  My preference here would be zoo since
it can use the Date class which is particularly suitable for daily
data:

	require(zoo)
	z <- read.table("myseries.dat") 
	colnames(z) = c("date", "value")  
	z <- zoo(z$value, as.Date(as.character(z$date), "%Y%m%d"))
	plot(z)




More information about the R-help mailing list