3
votes

I have a data frame that looks like this:

> head(hpr)
    Week      Hpr
1 199536 143111.0
2 199537 140721.9
3 199538 140864.9
4 199539 143293.1
5 199540 148913.3
6 199541 149267.6

I would like to convert this weekly data frame to a daily xts object. The answer to this question seem to solve this.

The problem here is the non standard date format, YYYYWW. I tried converting the dates with as.POSIXct but that got me nowhere:

index <- as.POSIXct(strptime(hpr[,1], "%Y%W"))
> head(index)
[1] "1995-05-28 CEST" "1995-05-28 CEST" "1995-05-28 CEST" "1995-05-28 CEST" "1995-05-28 CEST" "1995-05-28 CEST"

I wish to make an xts object that looks like this:

    Date      Hpr
1995-09-04 143111.0
1995-09-05 143111.0
1995-09-06 143111.0
1995-09-07 143111.0
1995-09-08 143111.0
1995-09-09 143111.0
1995-09-10 143111.0
1995-09-11 140721.9
1995-09-12 140721.9
1995-09-13 140721.9
1995-09-14 140721.9
1995-09-15 140721.9
1995-09-16 140721.9
1995-09-17 140721.9

Suggestions?

1
This isn't really a problem with xts; it's an issue with how to use strptimeJoshua Ulrich
Yes, you are of course correct. I should have focused on the 'strptime' function rather than the 'xts' functiontfunk

1 Answers

4
votes

See this question for a detailed discussion of this issue; the workaround I propose is

hpr$Week <- paste0(hpr$Week, "1")
ind <- as.POSIXct(strptime(hpr[,1], "%Y%W%u"))
#[1] "1995-09-04 MSD" "1995-09-11 MSD" "1995-09-18 MSD" "1995-09-25 MSK" "1995-10-02 MSK" "1995-10-09 MSK"
na.locf(merge(xts(hpr$Hpr, ind), 
              xts(, seq(start(w), end(w), "days"))))