Reputation:
I have an xts time series of weekly values
Jan 4 2004, 0.99
Jan 11 2004, 1.11
Jan 18 2004, 1.06
....
and I want to covert it to daily values
Jan 4 2004, 0.99
Jan 5 2004, 0.99
Jan 6 2004, 0.99
....
Jan 10 2004, 0.99
Jan 11 2004, 1.11
Jan 12 2004, 1.11
Jan 13 2004, 1.11
....
where each value is replicated for the following 6 days.
How can I do this in R?
Upvotes: 5
Views: 1650
Reputation: 176718
The data you show are not an xts series. I assume that is how the data are represented in a CSV file. To answer your question, I'm going to assume you have a weekly xts object, w
.
Merge w
with an empty xts object with an index that spans all the days you want. Then use na.locf
on the result.
d <- merge(w, xts(,seq(start(w),end(w),"days")))
d <- na.locf(d)
Upvotes: 12
Reputation: 110054
Here is one approach:
dat <- read.table(text="Jan 4 2004, 0.99
Jan 11 2004, 1.11
Jan 18 2004, 1.06", header=F)
dat
dat2 <- dat[rep(seq_len(nrow(dat)), 7), ]
dat2 <- with(dat2, dat2[order(V1, V2, V3), ])
dat2$V2 <- rep(dat$V2, each=7) + 0:6
rownames(dat2) <- 1:nrow(dat2)
dat2
Which gives:
V1 V2 V3 V4
1 Jan 4 2004, 0.99
2 Jan 5 2004, 0.99
3 Jan 6 2004, 0.99
4 Jan 7 2004, 0.99
5 Jan 8 2004, 0.99
6 Jan 9 2004, 0.99
7 Jan 10 2004, 0.99
8 Jan 11 2004, 1.11
9 Jan 12 2004, 1.11
10 Jan 13 2004, 1.11
11 Jan 14 2004, 1.11
12 Jan 15 2004, 1.11
13 Jan 16 2004, 1.11
14 Jan 17 2004, 1.11
15 Jan 18 2004, 1.06
16 Jan 19 2004, 1.06
17 Jan 20 2004, 1.06
18 Jan 21 2004, 1.06
19 Jan 22 2004, 1.06
20 Jan 23 2004, 1.06
21 Jan 24 2004, 1.06
Upvotes: 0