Trouble with zoo + POSIXct/POSIXlt

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble with zoo + POSIXct/POSIXlt

icosa atropa
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.

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)
> z
             V2      V3
2003-07-23 9.35  98.027
2003-07-23 9.32  97.954
> summary(z)
Error in "row.names<-.data.frame"(`*tmp*`, value = NULL) :
        invalid 'row.names' length
#format= doesn't seem to work here, error means index not #unique?
#
#Second try,
#This works:
>x=read.table(myfile, sep=',' , skip=30,\
x[,1]=as.POSIXct(strptime(x[,1], format=my.format,\ tz='UTC+6'))
> 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?
TIA,
christian

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with zoo + POSIXct/POSIXlt

Achim Zeileis
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

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with zoo + POSIXct/POSIXlt

Gabor Grothendieck
In reply to this post by icosa atropa
Don't know what z is but if its supposed to be x then, yes,
you have created an invalid zoo object since the index entries
must be unique.  The second try does not make sense since
x[,1]= is not a valid argument to read.zoo and the third one
is erroneous as it specifies an expression whereas it should
specify a function.

I think you want:

read.zoo(myfile, sep = ',' , FUN = function(x) as.POSIXct(strptime(x,
my.format)))

or read it in using the its package and then use xx <- as.zoo(x) where x is
the its object.

On 4/18/06, icosa atropa <[hidden email]> 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.
>
> 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)
> > z
>             V2      V3
> 2003-07-23 9.35  98.027
> 2003-07-23 9.32  97.954
> > summary(z)
> Error in "row.names<-.data.frame"(`*tmp*`, value = NULL) :
>        invalid 'row.names' length
> #format= doesn't seem to work here, error means index not #unique?
> #
> #Second try,
> #This works:
> >x=read.table(myfile, sep=',' , skip=30,\
> x[,1]=as.POSIXct(strptime(x[,1], format=my.format,\ tz='UTC+6'))
> > 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?
> TIA,
> christian
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance