[R] Construct time series objects from raw data stored in csv files

Gabor Grothendieck ggrothendieck at gmail.com
Thu Apr 12 21:26:45 CEST 2007


On 4/12/07, tom soyer <tom.soyer at gmail.com> wrote:
> Hi,
>
> I have time series data stored in csv files (see below for an example of the
> data). I understand that in order to analyze my data in R, I need to first
> transform it into a ts object. Howeve, I could not find an example on how
> exactly to do that. Is ts the only function I need? What are the steps that
> I need to go through to build a time series object from raw data like this?
>

Try pasting this into an R session:



Lines.raw <- "DATE,VALUE
1921-01-01,19.000
1921-02-01,18.400
1921-03-01,18.300
1921-04-01,18.100
1921-05-01,17.700
1921-06-01,17.600
1921-07-01,17.700
1921-08-01,17.700
1921-09-01,17.500
1921-10-01,17.500
1921-11-01,17.400
1921-12-01,17.300
1922-01-01,16.900
1922-02-01,16.900
1922-03-01,16.700
1922-04-01,16.700
1922-05-01,16.700
1922-06-01,16.700
1922-07-01,16.800
1922-08-01,16.600
1922-09-01,16.600
1922-10-01,16.700
1922-11-01,16.800
1922-12-01,16.900
"
library(zoo)
# replace next line with something like this:
#  z <- read.zoo("myfile.dat", header = TRUE, sep = ",")
z <- read.zoo(textConnection(Lines.raw), header = TRUE, sep = ",")
time(z) <- as.yearmon(time(z))
as.ts(z)



More information about the R-help mailing list