How to format data for time-series analysis
Just add the steps to do the conversion yourself. Also you data seems to be missing columns, at least in the mail you sent. Also the 'zoo' object will be a character matrix.
x <- read.table(tc <- textConnection(" Time Event State Value
+ 24-10-20 BMU 20 + 25-10-20 Image 2 + 26-10-20 BMU 10 + 27-10-20 BMU 11 + 28-10-20 Image 3 + 29-10-20 DPMS Begin NA + 30-10-20 Dream Begin NA + 31-10-20 BMU 3 + 1-11-20 Image 4 + 2-11-20 BMU 50 + 3-11-20 BMU 20 + 4-11-20 DPMS End NA + 5-11-20 Dream End NA + "), fill=TRUE, header=TRUE)
close(tc) # convert the times x$Time <- as.Date(as.character(x$Time), "%d-%m-%y") # convert to zoo require(zoo) x.z <- zoo(x[-1], x[[1]]) str(x.z)
chr [1:13, 1:3] "BMU" "Image" "BMU" "BMU" "Image" "DPMS" "Dream" "BMU" "Image" ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:13] "1" "2" "3" "4" ... ..$ : chr [1:3] "Event" "State" "Value" - attr(*, "index")=Class 'Date' num [1:13] 18559 18560 18561 18562 18563 ...
x.z
Event State Value 2020-10-24 BMU 20 <NA> 2020-10-25 Image 2 <NA> 2020-10-26 BMU 10 <NA> 2020-10-27 BMU 11 <NA> 2020-10-28 Image 3 <NA> 2020-10-29 DPMS Begin <NA> 2020-10-30 Dream Begin <NA> 2020-10-31 BMU 3 <NA> 2020-11-01 Image 4 <NA> 2020-11-02 BMU 50 <NA> 2020-11-03 BMU 20 <NA> 2020-11-04 DPMS End <NA> 2020-11-05 Dream End <NA>
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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?