[R-sig-finance] Trouble with zoo + POSIXct/POSIXlt
Achim Zeileis
Achim.Zeileis at wu-wien.ac.at
Tue Apr 18 13:04:53 CEST 2006
On Mon, 17 Apr 2006, icosa atropa wrote:
> After successfully using the 'its' package, I've been trying out 'zoo'
> for some of its more advanced functions. I can coerce my its objects
> into zoo objects without problem.
> Yet I'm having trouble understanding the read.zoo function's errors.
> It seems that my index class, POSIXct, is the problem.
POSIXct is fine, but you not used the arguments of read.zoo appropriately,
check ?read.zoo again and also see below.
> I have csv files like this:
> 7/23/2003 4:05:51 PM,9.35,98.027
> 7/23/2003 4:20:51 PM,9.32,97.954
>
> -----------------------
> #First try:
> >my.format = "%m/%d/%Y %I:%M:%S %p"
> >x=read.zoo(myfile, sep=',', skip=30, format=my.format)
The format argument does not do what you think it does. It is passed to
as.Date(), hence "Date" is used for the indexes, resulting in non-unique
indexes (because both rows are from the same day).
> #Second try,
> #This works:
> >x=read.table(myfile, sep=',' , skip=30,\
> x[,1]=as.POSIXct(strptime(x[,1], format=my.format,\ tz='UTC+6'))
That function is what you need:
myfun <- function(x)
as.POSIXct(strptime(x, format = "%m/%d/%Y %I:%M:%S %p", tz = "UTC+6"))
and then you can do
z <- read.zoo(myfile, FUN = myfun, sep = ",", skip = 30)
the function above is then used for transforming the first column.
> > y=zoo(x[,2:3], order.by=x[,1])
> #
> #but this doesn't:
> #
> >x=read.zoo(my.file, sep=',',skip=30,\
> FUN=as.POSIXct(strptime(as.character(x), format=my.format,
> tz='UTC+6')) )
> Error in read.zoo(files[1], sep = ",", skip = 30, FUN =
> as.POSIXct(strptime(as.character(x), :
> couldn't find function "FUN"
> ---------
> Any idea what the last error message means?
You have not passed a function to FUN, but a function call.
Best,
Z
More information about the R-sig-finance
mailing list