Hello! I have a csv file of intra-day financial data (5-min closing prices) that looks like this: (obs - the dates are formated as day/month/year, as is usual here in Brazil) Date;Time;Close 01/09/2009;10:00;56567 01/09/2009;10:05;56463 01/09/2009;10:10;56370 ##(goes on all day) 01/09/2009;16:45;55771 01/09/2009;16:50;55823 01/09/2009;16:55;55814 ##(jumps to the subsequent day) 02/09/2009;10:00;55626 02/09/2009;10:05;55723 02/09/2009;10:10;55659 ##(goes on all day) 02/09/2009;16:45;55742 02/09/2009;16:50;55717 02/09/2009;16:55;55385 ## (and so on to the next day) I would like to store the intra-day 5-min prices into a list, where each element would represent one day, something like list[[1]] "price at time 1, day 1" "price at time 2, day 1" ... "price at time n_1, day 1" list[[2]] "price at time 1, day 2" "price at time 2, day 2" ... "price at time n_2, day 2" and so on. As the "n_1", "n_2", etc, suggest, each day have its own number of observations (this reflects the fact that the market may open and close at varying daytimes). I have guessed that a list would be a better way to store my data, since a matrix would be filled with NA's and that is not exactly what I'm looking for. Thanks in advance, and best regards, Eduardo Horta [[alternative HTML version deleted]]
Try this:> x <- structure(list(Date = c("01/09/2009", "01/09/2009", "01/09/2009",+ "01/09/2009", "01/09/2009", "01/09/2009", "02/09/2009", "02/09/2009", + "02/09/2009", "02/09/2009", "02/09/2009", "02/09/2009"), Time = c("10:00", + "10:05", "10:10", "16:45", "16:50", "16:55", "10:00", "10:05", + "10:10", "16:45", "16:50", "16:55"), Close = c(56567L, 56463L, + 56370L, 55771L, 55823L, 55814L, 55626L, 55723L, 55659L, 55742L, + 55717L, 55385L)), .Names = c("Date", "Time", "Close"), class "data.frame", row.names = c(NA, + -12L))> # convert the time > x$time <- as.POSIXct(paste(x$Date, x$Time), format='%d/%m/%Y %H:%M') > # create list > x.list <- split(x[, c('time','Close')], format(x$time, format = "%Y%m%d")) > str(x.list)List of 2 $ 20090901:'data.frame': 6 obs. of 2 variables: ..$ time : POSIXct[1:6], format: "2009-09-01 10:00:00" "2009-09-01 10:05:00" "2009-09-01 10:10:00" ... ..$ Close: int [1:6] 56567 56463 56370 55771 55823 55814 $ 20090902:'data.frame': 6 obs. of 2 variables: ..$ time : POSIXct[1:6], format: "2009-09-02 10:00:00" "2009-09-02 10:05:00" "2009-09-02 10:10:00" ... ..$ Close: int [1:6] 55626 55723 55659 55742 55717 55385 On Sat, Nov 27, 2010 at 5:02 PM, Eduardo de Oliveira Horta <eduardo.oliveirahorta at gmail.com> wrote:> Hello! > > I have a csv file of intra-day financial data (5-min closing prices) that > looks like this: (obs - the dates are formated as day/month/year, as is > usual here in Brazil) > > Date;Time;Close > 01/09/2009;10:00;56567 > 01/09/2009;10:05;56463 > 01/09/2009;10:10;56370 > ##(goes on all day) > 01/09/2009;16:45;55771 > 01/09/2009;16:50;55823 > 01/09/2009;16:55;55814 > ##(jumps to the subsequent day) > 02/09/2009;10:00;55626 > 02/09/2009;10:05;55723 > 02/09/2009;10:10;55659 > ##(goes on all day) > 02/09/2009;16:45;55742 > 02/09/2009;16:50;55717 > 02/09/2009;16:55;55385 > ## (and so on to the next day) > > I would like to store the intra-day 5-min prices into a list, where each > element would represent one day, something like > list[[1]] > "price at time 1, day 1" > "price at time 2, day 1" > ... > "price at time n_1, day 1" > > list[[2]] > "price at time 1, day 2" > "price at time 2, day 2" > ... > "price at time n_2, day 2" > > and so on. > > As the "n_1", "n_2", etc, suggest, each day have its own number of > observations (this reflects the fact that the market may open and close at > varying daytimes). I have guessed that a list would be a better way to store > my data, since a matrix would be filled with NA's and that is not exactly > what I'm looking for. > > Thanks in advance, and best regards, > > Eduardo Horta > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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 that you are trying to solve?
On Sat, Nov 27, 2010 at 5:02 PM, Eduardo de Oliveira Horta <eduardo.oliveirahorta at gmail.com> wrote:> Hello! > > I have a csv file of intra-day financial data (5-min closing prices) that > looks like this: (obs - the dates are formated as day/month/year, as is > usual here in Brazil) > > Date;Time;Close > 01/09/2009;10:00;56567 > 01/09/2009;10:05;56463 > 01/09/2009;10:10;56370 > ##(goes on all day) > 01/09/2009;16:45;55771 > 01/09/2009;16:50;55823 > 01/09/2009;16:55;55814 > ##(jumps to the subsequent day) > 02/09/2009;10:00;55626 > 02/09/2009;10:05;55723 > 02/09/2009;10:10;55659 > ##(goes on all day) > 02/09/2009;16:45;55742 > 02/09/2009;16:50;55717 > 02/09/2009;16:55;55385 > ## (and so on to the next day) >Try this: library(zoo) library(chron) f <- function(x) times(paste(x, 0, sep = ":")) z <- read.zoo(textConnection(Lines), header = TRUE, sep = ";", split 1, index = 2, FUN = f) colnames(z) <- sub("X(..).(..).(....)", "\\3-\\2-\\1", colnames(z)) It produces a zoo object where each column is a series made up of one day. The columns are aligned by time of day so the result for the example data is the following zoo object:> z2009-09-01 2009-09-02 10:00:00 56567 55626 10:05:00 56463 55723 10:10:00 56370 55659 16:45:00 55771 55742 16:50:00 55823 55717 16:55:00 55814 55385 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
I think all you need is ?split -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Eduardo de Oliveira Horta Sent: Sunday, 28 November 2010 8:02 AM To: r-help at r-project.org Subject: [R] Two time measures Hello! I have a csv file of intra-day financial data (5-min closing prices) that looks like this: (obs - the dates are formated as day/month/year, as is usual here in Brazil) Date;Time;Close 01/09/2009;10:00;56567 01/09/2009;10:05;56463 01/09/2009;10:10;56370 ##(goes on all day) 01/09/2009;16:45;55771 01/09/2009;16:50;55823 01/09/2009;16:55;55814 ##(jumps to the subsequent day) 02/09/2009;10:00;55626 02/09/2009;10:05;55723 02/09/2009;10:10;55659 ##(goes on all day) 02/09/2009;16:45;55742 02/09/2009;16:50;55717 02/09/2009;16:55;55385 ## (and so on to the next day) I would like to store the intra-day 5-min prices into a list, where each element would represent one day, something like list[[1]] "price at time 1, day 1" "price at time 2, day 1" ... "price at time n_1, day 1" list[[2]] "price at time 1, day 2" "price at time 2, day 2" ... "price at time n_2, day 2" and so on. As the "n_1", "n_2", etc, suggest, each day have its own number of observations (this reflects the fact that the market may open and close at varying daytimes). I have guessed that a list would be a better way to store my data, since a matrix would be filled with NA's and that is not exactly what I'm looking for. Thanks in advance, and best regards, Eduardo Horta [[alternative HTML version deleted]] ______________________________________________ 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.