[R] zoo.read intraday data

Immanuel mane.desk at googlemail.com
Mon Oct 25 17:44:16 CEST 2010


Thanks,

by now I ran already into the next problem, while trying to convert
the zoo to an xts object.
Somehow the timestamps get lost, no idea why.

I did read through the vignettes, but got confused
by all this, chron, as.POSIXct, zoo and xts stuff.

with your example I got:
Error in .subset(x, j) : invalid subscript type 'list'


best regards
-------------------------

# TODO: Add comment
#
# Author: flyingimmi
###############################################################################


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
        06.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], "00", sep = ":"),
            format = c(dates = "d.m.y", times = "h:m:s"))


# 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)

intradayData <- xts(z, time(z))


print(z)

print(intradayData)
---------------------------------------------------
output:

                     Open  High   Low Close Up Down
(05.02.01 00:30:00) 421.2 421.2 421.2 421.2 11    0
(05.02.01 01:30:00) 421.2 421.4 421.2 421.4  7    0
(05.02.01 02:30:00) 421.6 421.6 421.5 421.5 26    1
(06.02.01 02:00:00) 421.3 421.3 421.3 421.3  0    5
      Open  High   Low Close Up Down
<NA> 421.2 421.2 421.2 421.2 11    0
<NA> 421.2 421.4 421.2 421.4  7    0
<NA> 421.6 421.6 421.5 421.5 26    1
<NA> 421.3 421.3 421.3 421.3  0    5

On 10/25/2010 03:47 PM, Gabor Grothendieck wrote:
> 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)
>
>



More information about the R-help mailing list