Hello all, I'm trying to use zoo.read but can't figure out how to deal with the time format. (example below) would be nice if someone could help. best regards, Immanuel --------------------------- L <- "Date,Time,Open,High,Low,Close,Up,Down 05.02.2001,00:30,421.20,421.20,421.20,421.20,11,0 05.02.2001,01:30,421.20,421.40,421.20,421.40,7,0 05.02.2001,02:00,421.30,421.30,421.30,421.30,0,5 05.02.2001,02:30,421.60,421.60,421.50,421.50,26,1" library(zoo) library(chron) f <- function(x) chron(paste(x[,1]),paste(x[,2]), format = c(dates = "D.M.Y", times = "hh:mm")) z <- read.zoo(textConnection(L), index = 1:2, sep=",", header = TRUE, FUN = f) print(z)
On Mon, 25 Oct 2010, Immanuel wrote:> Hello all, > > I'm trying to use zoo.readJust for the record: read.zoo().> but can't figure out > how to deal with the time format. (example below)Yes, the problem is only the chron conversion (and not read.zoo).> would be nice if someone could help. > > best regards, > Immanuel > > --------------------------- > L <- "Date,Time,Open,High,Low,Close,Up,Down > 05.02.2001,00:30,421.20,421.20,421.20,421.20,11,0 > 05.02.2001,01:30,421.20,421.40,421.20,421.40,7,0 > 05.02.2001,02:00,421.30,421.30,421.30,421.30,0,5 > 05.02.2001,02:30,421.60,421.60,421.50,421.50,26,1" > > library(zoo) > library(chron) > > f <- function(x) chron(paste(x[,1]),paste(x[,2]), format > = c(dates = "D.M.Y", times = "hh:mm"))I think that "chron" might require seconds. Hence you can add 00 seconds for all times and change the format accordingly, e.g., f <- function(x) chron(paste(x[,1]), paste(x[,2], "00", sep = ":"), format = c(dates = "d.m.y", times = "h:m:s")) Then, the call below should work ok. hth, Z> z <- read.zoo(textConnection(L), index = 1:2, sep=",", header = TRUE, > FUN = f) > > print(z) > > ______________________________________________ > 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. >
Hey, work's like a charm. thanks
On Mon, Oct 25, 2010 at 9:02 AM, Immanuel <mane.desk at googlemail.com> wrote:> Hello all, > > I'm trying to use zoo.read but can't figure out > how to deal with the time format. (example below) > > would be nice if someone could help. > > best regards, > Immanuel > > --------------------------- > L <- "Date,Time,Open,High,Low,Close,Up,Down > ? ? ? ?05.02.2001,00:30,421.20,421.20,421.20,421.20,11,0 > ? ? ? ?05.02.2001,01:30,421.20,421.40,421.20,421.40,7,0 > ? ? ? ?05.02.2001,02:00,421.30,421.30,421.30,421.30,0,5 > ? ? ? ?05.02.2001,02:30,421.60,421.60,421.50,421.50,26,1" > > library(zoo) > library(chron) > > f <- function(x) chron(paste(x[,1]),paste(x[,2]), format > ? ? ? ? ? ? ? ? ? ?= c(dates = "D.M.Y", times = "hh:mm")) > > z <- read.zoo(textConnection(L), index = 1:2, sep=",", header = TRUE, > FUN ?= f) > > print(z)Here are a few more possibilities: # use chron appending seconds and index = list(1, 2) f <- function(d, t, format = c("m.d.y", "h:m:s")) { chron(d, paste(t, "00", sep = ":"), format = format) } z <- read.zoo(textConnection(L), index = list(1, 2), sep=",", header TRUE, FUN = f) # use as.chron and index = list(1, 2) f2 <- function(d, t, format = "%d.%m.%Y %H:%M") { as.chron(paste(d, t), format = format) } z2 <- read.zoo(textConnection(L), index = list(1, 2), sep=",", header = TRUE, FUN = f) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On Tue, Dec 21, 2010 at 11:36 PM, szimine <szimine at gmail.com> wrote:> > Hi Gabor et al. > > the > ?f3 <- function(...) as.POSIXct(paste(...), format = "%Y%m%d %H:%M:%S" ) > > helped me to read intraday data from file > ###### > <TICKER>,<NAME>,<PER>,<DATE>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OPENINT> > ICE.BRN,ice.brn_m5,5,20100802,10:40:00,79.21000,79.26000,79.16000,79.20000,238,0 > ICE.BRN,ice.brn_m5,5,20100802,10:45:00,79.19000,79.26000,79.19000,79.21000,413,0 > ###### > > ##intraday data 5m ?file > fnameId= "./finam_brn_m5.csv" > pDateTimeColumns <- list(4,5) > b <- read.zoo(fnameId, index=pDateTimeColumns , sep=",", header=TRUE, > FUN=f3 ? ) > xb <- as.xts(b) > > >> head(b,2) ## > ? ? ? ? ? ? ? ? ? ?X.TICKER. X.NAME. ? ?X.PER. X.OPEN. X.HIGH. X.LOW. > X.CLOSE. X.VOL. X.OPENINT. > 2010-08-02 10:40:00 ICE.BRN ? ice.brn_m5 5 ? ? ?79.21 ? 79.26 ? 79.16 ?79.20 > 238 ? 0 > 2010-08-02 10:45:00 ICE.BRN ? ice.brn_m5 5 ? ? ?79.19 ? 79.26 ? 79.19 ?79.21 > 413 ? 0 > > problem is that after the conversion to xts ?numeric values got converted to > chars > >> head(xb,2) > ? ? ? ? ? ? ? ? ? ?X.TICKER. X.NAME. ? ? ?X.PER. X.OPEN. X.HIGH. X.LOW. > X.CLOSE. X.VOL. X.OPENINT. > 2010-08-02 10:40:00 "ICE.BRN" "ice.brn_m5" "5" ? ?"79.21" "79.26" "79.16" > "79.20" ?" 238" "0" > 2010-08-02 10:45:00 "ICE.BRN" "ice.brn_m5" "5" ? ?"79.19" "79.26" "79.19" > "79.21" ?" 413" "0" >Read it all in using read.csv and the reread it using read.zoo (note that read.zoo can read data.frames) excluding the bad columns or else use the colClasses argument to suppress the unwanted column(s). Here are 4 methods. Lines <- "<TICKER>,<NAME>,<PER>,<DATE>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OPENINT> ICE.BRN,ice.brn_m5,5,20100802,10:40:00,79.21000,79.26000,79.16000,79.20000,238,0 ICE.BRN,ice.brn_m5,5,20100802,10:45:00,79.19000,79.26000,79.19000,79.21000,413,0" library(zoo) # method 1. read.csv/read.zoo with split= and removing col 2 DF <- read.csv(textConnection(Lines)) z1 <- read.zoo(DF[-2], split = 1, index = list(3, 4), FUN = f3) # method 2. read.csv/read.zoo removing col 1 and 2 # this one only works if there is one ticker DF <- read.csv(textConnection(Lines)) z2 <- read.zoo(DF[-(1:2)], index = list(2, 3), FUN = f3) # method 3. read.zoo with colClasses as in #1 colClasses <- c("character", "NULL", "numeric", "character", "character", rep("numeric", 6)) z3 <- read.zoo(textConnection(Lines), header = TRUE, sep = ",", split = 1, index = list(3, 4), FUN = f3, colClasses = colClasses) #4. A method similar to #2 could also be used based on colClasses. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com