[R] working with zoo time index ??

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 16 15:24:50 CEST 2010


On Wed, Jun 16, 2010 at 8:36 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Wed, Jun 16, 2010 at 7:58 AM, skan <juanpide at gmail.com> wrote:
>>
>> Hi
>> thanks
>>
>>
>> Let say data are written like this:
>> 1990-01-01 10:01:00 ,  0.910
>> 1990-01-01 10:03:00 ,  0.905
>>
>> Would it be ok to read it with theses lines or is better to use your way?
>>
>> tmp <- read.table("demo2.txt", sep = ",")
>> z <- zoo(tmp[, 2], as.Date(as.chron(tmp[, 1]), format = "%Y-%m-%d
>> %H:%M:%S"))
>
> You want to avoid creating zoo objects that have duplicate times since
> they can't be merged.  This will read the data in and at the same time
> aggregate it by date using the first value among all values with the
> same date.
>
> Lines <- "1990-01-01 10:01:00 ,  0.910
> 1990-01-01 10:03:00 ,  0.905
> 1990-01-02 10:03:00 ,  0.895"
>
> library(zoo)
> z <- read.zoo(textConnection(Lines), sep = ",", FUN = as.Date,
>        aggregate = function(x) head(x, 1))
>

This can be reduced slightly to the following as it defaults to Date here:

z <- read.zoo(textConnection(Lines), sep = ",", aggregate =
function(x) head(x, 1))



More information about the R-help mailing list