Extend a weekly times series into daily

无人久伴 提交于 2019-12-05 11:25:59

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)

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!