Skip to content
Prev 278627 / 398503 Next

Missing data?

On Sun, Nov 27, 2011 at 4:08 PM, Kevin Burton <rkevinburton at charter.net> wrote:
OK. As you are willing to regard days past the 364th as part of the
last week of the year then we can do this.

Create a zoo object z as test data.   Then convert its time scale to
year + week/52 where 0 is the first week of the year and we replace
any week that is greater than 51 with 51.  Then we aggregate z by week
taking the last data point in the week and convert it to ts.  Because
of the way we constructed it the frequency will be 52.

library(zoo)

# test data
z <- zoo(1:100, Sys.Date() + 1:100)

yr.wk <- with(as.POSIXlt(time(z)), year + 1900 + pmin(yday %/% 7, 51) / 52)
z.wk <- aggregate(z, yr.wk, tail, 1)
z.ts <- as.ts(z.wk)

frequency(z.ts) # 52