Hi, I am trying to load a data file that looks like this: |Date,Time,Open,High,Low,Close,Up,Down 05/02/2001,0030,421.20,421.20,421.20,421.20,11,0 05/02/2001,0130,421.20,421.40,421.20,421.40,7,0 05/02/2001,0200,421.30,421.30,421.30,421.30,0,5 05/02/2001,0230,421.60,421.60,421.50,421.50,26,1| etc. into an R timeseries or ts object. The key point is that both the date and time need to become part of the index. With zoo, this line will load the data: z <- read.zoo("foo_hs.csv", format = "%m/%d/%Y", sep=",", header = TRUE ) but the Time does not become part of the index this way. This means the index is non-unique, and that is not the goal. Could someone kindly show me a way, using R itself, to deal with the separate Date and Time columns so as to properly combine them into the index for the timeseries? Thanks!
In zoo the index= argument to read.zoo can be a vector of column indices to indicate that the time is split across multiple columns and the FUN= argument can be used to process the multiple columns. In this example the resulting z uses chron: L <- "Date,Time,Open,High,Low,Close,Up,Down 05/02/2001,0030,421.20,421.20,421.20,421.20,11,0 05/02/2001,0130,421.20,421.40,421.20,421.40,7,0 05/02/2001,0200,421.30,421.30,421.30,421.30,0,5 05/02/2001,0230,421.60,421.60,421.50,421.50,26,1" library(zoo) library(chron) f <- function(x) chron(paste(x[,1]), sprintf("%04d00", x[,2]), format = c("M/D/Y", "HMS")) # z <- read.zoo("myfile.csv", index = 1:2, sep=",", header = TRUE, FUN = f) z <- read.zoo(textConnection(L), index = 1:2, sep=",", header = TRUE, FUN = f) On Sun, May 16, 2010 at 7:22 AM, Steve Johns <steve.johns at verizon.net> wrote:> Hi, > > I am trying to load a data file that looks like this: > > |Date,Time,Open,High,Low,Close,Up,Down > 05/02/2001,0030,421.20,421.20,421.20,421.20,11,0 > 05/02/2001,0130,421.20,421.40,421.20,421.40,7,0 > 05/02/2001,0200,421.30,421.30,421.30,421.30,0,5 > 05/02/2001,0230,421.60,421.60,421.50,421.50,26,1| > etc. > > into an R timeseries or ts object. > > The key point is that both the date and time need to become part of the > index. > > With zoo, this line will load the data: > > z <- read.zoo("foo_hs.csv", format = "%m/%d/%Y", sep=",", header = TRUE ) > > but the Time does not become part of the index this way. ?This means the > index is non-unique, and that is not the goal. > > Could someone kindly show me a way, using R itself, to deal with the > separate Date and Time columns so as to properly combine them into the index > for the timeseries? > > Thanks! > > ______________________________________________ > 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. >
Hi Steve, I "think" what you want to do is get a unique time-date from the first two columns. Try something like this: (changing the file name obviously. mydate should give you a time and date format that you can add to the existing data.frame. mydata <- read.table("C:/rdata/dates.junk.csv", header=TRUE, sep=",", colClasses=c("character","character", "numeric" , "numeric", "numeric", "numeric", "numeric", "numeric")) df1 <- paste(mydata[,1], "", mydata[,2]) mydates <- strptime(df1, "%d/%m/%Y %H%M") --- On Sun, 5/16/10, Steve Johns <steve.johns at verizon.net> wrote:> From: Steve Johns <steve.johns at verizon.net> > Subject: [R] Loading Intraday Time Series Data > To: r-help at r-project.org > Received: Sunday, May 16, 2010, 7:22 AM > Hi, > > I am trying to load a data file that looks like this: > > |Date,Time,Open,High,Low,Close,Up,Down > 05/02/2001,0030,421.20,421.20,421.20,421.20,11,0 > 05/02/2001,0130,421.20,421.40,421.20,421.40,7,0 > 05/02/2001,0200,421.30,421.30,421.30,421.30,0,5 > 05/02/2001,0230,421.60,421.60,421.50,421.50,26,1| > etc. > > into an R timeseries or ts object. > > The key point is that both the date and time need to become > part of the index. > > With zoo, this line will load the data: > > z <- read.zoo("foo_hs.csv", format = "%m/%d/%Y", > sep=",", header = TRUE ) > > but the Time does not become part of the index this > way.? This means the index is non-unique, and that is > not the goal. > > Could someone kindly show me a way, using R itself, to deal > with the separate Date and Time columns so as to properly > combine them into the index for the timeseries? > > Thanks! > > ______________________________________________ > 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. >