cumsum on chron objects
On 5/17/05, Sebastian Luque <sluque at mun.ca> wrote:
Hi, Is there some alternative to cumsum for chron objects? I have data frames that contain some chron objects that look like this: DateTime 13/10/03 12:30:35 NA NA NA 15/10/03 16:30:05 NA NA ... and I've been trying to replace the NA's so that a date/time sequence is created starting with the preceding available value. Because the number of rows with NA's following each available date/time is unknown, I've split the data frame using: splitdf <- split(df, as.factor(df$DateTime)) so that I can later use lapply to work on each "block" of data. I thought I could use cumsum and set the NA's to the desired interval to create the date/time sequence starting with the first row. However, this function is not defined for chron objects. Does anybody know of alternatives to create such a sequence?
The 'zoo' package has na.locf which stands for Last Occurrence Carried Forward, which is what I believe you want. First let us create some test data, x:
library(chron); library(zoo) x <- chron(c(1.5, 2, NA, NA, 4, NA)) x
[1] (01/02/70 12:00:00) (01/03/70 00:00:00) (NA NA) [4] (NA NA) (01/05/70 00:00:00) (NA NA)
# na.locf is intended for zoo objects but we can convert # the chron object to zoo, apply na.locf and convert back:
chron(as.vector(na.locf(zoo(as.vector(x)))))
[1] (01/02/70 12:00:00) (01/03/70 00:00:00) (01/03/70 00:00:00) [4] (01/03/70 00:00:00) (01/05/70 00:00:00) (01/05/70 00:00:00)