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

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 22 18:11:30 CEST 2009


Here are a couple of possibilities assuming z is the result
of read.zoo:

> z[hours(time(z)) == 3,]
                       V2     V3     V4
(03/02/09 03:00:00) 124.2 124.21 124.11
> grep("03:00:00", time(z))
[1] 5
> z[grep("03:00:00", time(z)),]
                       V2     V3     V4
(03/02/09 03:00:00) 124.2 124.21 124.11


On Mon, Jun 22, 2009 at 12:02 PM, Michael<comtech.usa at gmail.com> wrote:
> Thanks so much and it worked wonders.
>
> Is it possible to select the rows based on some sort of wildcards,
> such as selecting out all the rows containing "3:00:00"?
>
> On Sun, Jun 21, 2009 at 8:21 AM, Gabor
> Grothendieck<ggrothendieck at gmail.com> wrote:
>> 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