[R] zoo.read intraday data

Gabor Grothendieck ggrothendieck at gmail.com
Mon Oct 25 15:47:53 CEST 2010


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



More information about the R-help mailing list