[R-SIG-Finance] how to read in this time series csv file with both dates and times?

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 21 17:21:15 CEST 2009


There are several things wrong here:

1. FUN= must be a function but its not in the posted code

2. as.chron() uses % codes in its format as described in
?strptime, not the format style for chron().

See R News 4/1 for more on dates and times and also
see the examples in ?read.zoo

Here are two ways to do it.  The first uses as.chron()
and the second defines a custom function, toChron,
that uses chron().

library(zoo)
library(chron)

Lines <- "3/1/2009 23:00:00,123.76,123.94,123.7
3/2/2009 0:00:00,123.85,124.16,123.85
3/2/2009 1:00:00,124.11,124.15,124.06
3/2/2009 2:00:00,124.14,124.32,124.12
3/2/2009 3:00:00,124.2,124.21,124.11
3/2/2009 4:00:00,124.16,124.18,123.94
3/2/2009 5:00:00,124.01,124.2,123.97"

# 1
read.zoo(textConnection(Lines), sep = ",", FUN = as.chron,
	format = "%m/%d/%Y %H:%M:%S")

# 2
toChron <- function(x) chron(sub(" .*", "", x), sub(".* ", "", x))
read.zoo(textConnection(Lines), sep = ",", FUN = toChron)

On Sun, Jun 21, 2009 at 2:42 AM, Michael<comtech.usa at gmail.com> wrote:
> Hi all,
>
> I want to read in this "csv" file,
>
> 3/1/2009 23:00:00,123.76,123.94,123.7
> 3/2/2009 0:00:00,123.85,124.16,123.85
> 3/2/2009 1:00:00,124.11,124.15,124.06
> 3/2/2009 2:00:00,124.14,124.32,124.12
> 3/2/2009 3:00:00,124.2,124.21,124.11
> 3/2/2009 4:00:00,124.16,124.18,123.94
> 3/2/2009 5:00:00,124.01,124.2,123.97
>
> And I got the following error message, what could be wrong?
>
> Thanks a lot!
>
>
> z <- read.zoo("prices.csv", header = TRUE, sep = ",", FUN =
> as.chron(format=c(dates = "m/d/y", times = "h:m:s")))
> Error in inherits(x, "chron") : element 1 is empty;
>   the part of the args list of '.Internal' being evaluated was:
>   (x, what, which)
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



More information about the R-SIG-Finance mailing list