[R] Unable to create/index a zoo irregular timeseries

Gabor Grothendieck ggrothendieck at gmail.com
Thu Feb 21 18:18:50 CET 2008


Here are two ways of doing it with POSIXct and one with chron.
Read RNews 4/1, ?read.zoo and the three zoo vignettes:

Lines <- '"Data","MAX","MIN","Note"
"07-01-2008 08:00:00", 135, 90, "Eccessi feste, inizio dieta"
"07-01-2008 18:00:00", 135, 85, ""
"08-01-2008 08:00:00", 125, 75, ""
'
library(zoo)
# above needed for all three

# 1
z <- read.zoo(textConnection(Lines), header = TRUE, sep = ",",
    colClasses = c(Note = "NULL"),
    FUN = function(x) as.POSIXct(strptime(x, "%m-%m-%Y %H:%M:%S")))

# 2
DF <- read.csv(textConnection(Lines))
z2 <- zoo(as.matrix(DF[2:3]), as.POSIXct(strptime(DF[,1], "%d-%m-%Y %H:%M:%S")))

# 3
library(chron)
zc <- read.zoo(textConnection(Lines), header = TRUE, sep = ",",
    colClasses = c(Note = "NULL"),
    FUN = function(x)
      chron(sub(" .*", "", x), sub(".* ", "", x), format = c("d-m-y", "H:M:S")))


On Thu, Feb 21, 2008 at 11:11 AM, vittorio <vdemart1 at tin.it> wrote:
> In the text file pressione2008.csv I have the following
>
> "Data","MAX","MIN","Note"
> "07-01-2008 08:00:00", 135, 90, "Eccessi feste, inizio dieta"
> "07-01-2008 18:00:00", 135, 85, ""
> "08-01-2008 08:00:00", 125, 75, ""
>
> which is a collection of blood pressure data at different time of the day.
> I would like to build an its with MIN & MAX blood pressure but being a real
> newbye with zoo I obtain the following
>
> > library(zoo)
> pressione <- data.frame(read.csv("pressione2008.csv"))
>

read.csv already gives a data frame

>  miedate <- strptime(as.character(pressione[,1]), format="%d-%m-%Y %H:%M:%S")
>
> > miedate
> [1] "2008-01-07 08:00:00" "2008-01-07 18:00:00" "2008-01-08 08:00:00"
>
> > str(miedate)
>  POSIXlt[1:9], format: "2008-01-07 08:00:00" "2008-01-07 18:00:00" ...

The POSIXlt still needs to be converted to POSIXct.

>
> > ts<- as.zoo(matrix(pressione[,2:3],ncol=2), miedate)
> > ts
> Error in Ops.POSIXt(freq, d) : * not defined for "POSIXt" objects
>
> > ts<- zoo(matrix(pressione[,2:3],ncol=2), miedate)
> Error in order(x, ..., na.last = na.last, decreasing = decreasing) :
>  unimplemented type 'list' in 'orderVector1'
> In addition: Warning message:
> In zoo(matrix(pressione[, 2:3], ncol = 2), miedate) :
>  some methods for "zoo" objects do not work if the index entries
> in 'order.by' are not unique
>
> >
>
> Please help
>
> Ciao
> Vittorio
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list