[R] Help regading time series data reading
Petr PIKAL
petr.pikal at precheza.cz
Wed Sep 12 10:52:49 CEST 2007
r-help-bounces at r-project.org napsal dne 12.09.2007 09:36:13:
> On Tue, 2007-09-11 at 19:56 +0300, Tolga Uzuner wrote:
> > Dear R-Users,
> >
> > Have a question about reading in some data and manipulating dates. I
> > have a data set in excel which looks like this:
> >
> > Date PEG ETN HSP PTC
> > 13/10/2004 41.92 64.75 29.86 9.27
> > 14/10/2004 41.93 61.79 29.98 9.14
> > 15/10/2004 41.69 62.7 30.09 9.04
> > 18/10/2004 41.37 62.14 30.39 8.96
> > 19/10/2004 41.01 61.98 29.61 9.02
> > 20/10/2004 41.01 61.98 30.25 9
> >
> > I have used read.table by saving the sheety above in tab format to
> > read this in but am
> > having some difficulties:
> > - the dates above do not seem to be getting read in in date format:
> > how can I force this ?
>
> For the first question (I'm not sure how to do the second):
Most probably merge can do the requested synchronization, especially with
all.** parametrs.
Regards
Petr
>
> We use read.table() to read in the data from file (here I saved your
> data as tab delimited file 'temp.csv'. Argument as.is = TRUE is used to
> stop R converting character strings (i.e. the Date column here) to
> factors; this saves us a step later converting them back.
>
> > dat <- read.table("temp.csv", sep = "\t", as.is = TRUE, header = TRUE)
>
> Data read in OK:
>
> > dat
> Date PEG ETN HSP PTC
> 1 13/10/2004 41.92 64.75 29.86 9.27
> 2 14/10/2004 41.93 61.79 29.98 9.14
> 3 15/10/2004 41.69 62.70 30.09 9.04
> 4 18/10/2004 41.37 62.14 30.39 8.96
> 5 19/10/2004 41.01 61.98 29.61 9.02
> 6 20/10/2004 41.01 61.98 30.25 9.00
>
> Use as.Date() to convert the character strings to a vector of class
> 'Date'. The format argument tells R how the dates are formatted in the
> character strings you just read in. The %d, %m bits etc are place
> holders describing the date parts, but the "/" characters are literal -
> if your dates were formatted "20-10-2004", you would use format = "%d-%
> m-%Y". See ?strftime for how to specify 'format' if you have dates
> formatted in other ways (say with month name instead of number).
>
> > dat$Date <- as.Date(dat$Date, format = "%d/%m/%Y")
>
> Note that now dat$Date is now an object of class 'Date':
>
> > str(dat)
> 'data.frame': 6 obs. of 5 variables:
> $ Date:Class 'Date' num [1:6] 12704 12705 12706 12709 12710 ...
> $ PEG : num 41.9 41.9 41.7 41.4 41.0 ...
> $ ETN : num 64.8 61.8 62.7 62.1 62.0 ...
> $ HSP : num 29.9 30.0 30.1 30.4 29.6 ...
> $ PTC : num 9.27 9.14 9.04 8.96 9.02 9
>
> Which R will treat appropriately in plots for example:
>
> > plot(PEG ~ Date, data = dat, type = "l")
>
> HTH
>
> G
>
> --
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> Gavin Simpson [t] +44 (0)20 7679 0522
> ECRC, UCL Geography, [f] +44 (0)20 7679 0565
> Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
> Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
> UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list