[R] How to format data for time-series analysis
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Oct 29 01:41:29 CET 2007
Some information on the meaning of the fields would have
been helpful but asuming that the fields are row number, dd-mm-yy date,
Event, State (which may be empty) and Value, the following
produces a list, L, of 3 zoo objects:
Lines.raw <- "Time Event State Value
1 24-10-20 BMU 20
2 25-10-20 Image 2
3 26-10-20 BMU 10
4 27-10-20 BMU 11
5 28-10-20 Image 3
6 29-10-20 DPMS Begin NA
7 30-10-20 Dream Begin NA
8 31-10-20 BMU 3
9 1-11-20 Image 4
10 2-11-20 BMU 50
11 3-11-20 BMU 20
12 4-11-20 DPMS End NA
13 5-11-20 Dream End NA
"
# In reality next line would be Lines <- readLines("myfile.dat")
Lines <- readLines(textConnection(Lines.raw))
# 10 spaces indicates State is empty so replace spaces with a dot
Lines <- sub(" ", " . ", Lines)
DF <- read.table(textConnection(Lines))
L <- lapply(DF[-1], zoo, order.by = as.Date(DF$Time, "%d-%m-%y"))
# Alternately combine everything into a single zoo object, z,
# but since zoo objects are factor or numeric we convert the factors
# to numeric:
z <- zoo(data.matrix(DF[-1]), as.Date(DF$Time, "%d-%m-%y"))
On 10/28/07, B. Bogart <bbogart at sfu.ca> wrote:
> Hey all again,
>
> So I'm going through tutorials and getting a better sense of R structures.
>
> So I made some mockup data to see if I can figure out how to load it
> properly. (attached) if anyone has any suggestions on a better way to
> structure that data please let me know.
>
> So the file has three columns, the date/time of the event, the event
> name, the event state (if there is one) and the event value (if there is
> one). I'm using the built-in date/time class of openoffice as a starting
> point for the date/time.
>
> I'm expecting to load this file as a data.frame where each column is a
> different class. Like so:
>
> > data <-
> read.table(file="testdata.csv",sep=",",header=TRUE,colClasses=c("zoo","factor","factor","numeric"))
>
> Unfortunately It seems there is no function for converting from
> "character" to "zoo":
>
> Error in methods::as(data[[i]], colClasses[i]) :
> no method or default for coercing "character" to "zoo"
>
> So I tried using POSIXct as suggested.
>
> Which does load the file, but it seems the times are truncated, leaving
> only the dates:
>
> > data
> Time Event State Value
> 1 24-10-20 BMU 20
> 2 25-10-20 Image 2
> ...3 26-10-20 BMU 10
> 4 27-10-20 BMU 11
> 5 28-10-20 Image 3
> 6 29-10-20 DPMS Begin NA
> 7 30-10-20 Dream Begin NA
> 8 31-10-20 BMU 3
> 9 1-11-20 Image 4
> 10 2-11-20 BMU 50
> 11 3-11-20 BMU 20
> 12 4-11-20 DPMS End NA
> 13 5-11-20 Dream End NA
>
> Same results as using "Date" as the colClass.
>
> Any advice?
>
> Thanks,
> B. Bogart
>
>
>
>
> ______________________________________________
> 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