Skip to content

Add missing values/timestamps

2 messages · Gabor Grothendieck, j.k

#
Try this where we read in a zoo series and then merge it with
a zero width regularly spaced series to create the result.

Lines <- "V1,V2
2008-10-14 08:45:00,94411.08
2008-10-14 08:50:00,90745.45
2008-10-14 08:55:00,82963.35
2008-10-14 09:00:00,75684.38
2008-10-14 09:05:00,78931.82
2008-10-14 09:20:00,74580.11
2008-10-14 09:25:00,69666.48
2008-10-14 09:30:00,77794.89"

library(zoo)
z <- read.zoo(textConnection(Lines), header = TRUE, sep = ",", tz = "")
tt <- seq(time(z)[1], time(z)[length(z)], 5*60)
merge(z, zoo(, tt))

The last statement's output will be:
2008-10-14 08:45:00 2008-10-14 08:50:00 2008-10-14 08:55:00 2008-10-14 09:00:00
           94411.08            90745.45            82963.35            75684.38
2008-10-14 09:05:00 2008-10-14 09:10:00 2008-10-14 09:15:00 2008-10-14 09:20:00
           78931.82                  NA                  NA            74580.11
2008-10-14 09:25:00 2008-10-14 09:30:00
           69666.48            77794.89


See the 3 zoo vignettes for use of zoo series and R News 4/1 (for
date/time info).
On Mon, Mar 30, 2009 at 10:38 AM, j.k <kathan at gmx.at> wrote:
j.k
#
Thanks a lot!
The way with zoo worked perfect.

Here is the code I've used finally:

data.input01 <-read.csv("./1_15min.txt", header = TRUE, sep = ";",
quote="\"", dec=",", fill = TRUE, comment.char="")
data.input02 <-read.csv("./2_15min.txt", header = TRUE, sep = ";",
quote="\"", dec=",", fill = TRUE, comment.char="")
data.input03 <-read.csv("./3_15min.txt", header = TRUE, sep = ";",
quote="\"", dec=",", fill = TRUE, comment.char="")
data.troughput01 <- rbind(data.input01,data.input02,data.input03)
data.test <- seq(as.POSIXct("2006-01-01 00:00:00"),as.POSIXct("2006-12-31
23:45:00"),900)
data.troughput02 <- as.zoo(data.troughput01$V2)
index(data.troughput02) <- as.POSIXct(data.troughput01$V1)
data.output01 <- merge(data.troughput04,zoo(,data.test))
data.output02 <- na.approx(data.output01,na.rm = FALSE)
Gabor Grothendieck wrote: