[R-SIG-Finance] 5th of month working day

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jan 18 16:16:56 CET 2010


On Mon, Jan 18, 2010 at 9:54 AM, Research <risk2009 at ath.forthnet.gr> wrote:
> Hello,
>
> I have a daily data zoo object with  prices such as:
>
> 05/04/2006      1311.56
> 06/04/2006      1309.04
> 07/04/2006      1295.5
> 10/04/2006      1296.6
> 11/04/2006      1286.57
> 12/04/2006      1288.12
> 13/04/2006      1289.12
> 14/04/2006      1289.12
> 17/04/2006      1285.33
> 18/04/2006      1307.65
> 19/04/2006      1309.93
> 20/04/2006      1311.46
> 21/04/2006      1311.28
> 24/04/2006      1308.11
> 25/04/2006      1301.74
> 26/04/2006      1305.41
> 27/04/2006      1309.72
> 28/04/2006      1310.61
> 01/05/2006      1305.19
> 02/05/2006      1313.21
> 03/05/2006      1307.85
> 04/05/2006      1312.25
> 05/05/2006      1325.76
>
>
>
> How can I isolate the 5th day of each month (if this was a
> working/trading day) otherwise the most recent (before the 5th) working
> day for each month?
>

Your sample data always has the 5th of the month filled in but
assuming that that is not the case for the real data, merge your
series with a zero width series having every date and use na.locf to
move values up into subsequent NAs.  Then just pick off the 5th of
each month.

Lines <- "05/04/2006      1311.56
06/04/2006      1309.04
07/04/2006      1295.5
10/04/2006      1296.6
11/04/2006      1286.57
12/04/2006      1288.12
13/04/2006      1289.12
14/04/2006      1289.12
17/04/2006      1285.33
18/04/2006      1307.65
19/04/2006      1309.93
20/04/2006      1311.46
21/04/2006      1311.28
24/04/2006      1308.11
25/04/2006      1301.74
26/04/2006      1305.41
27/04/2006      1309.72
28/04/2006      1310.61
01/05/2006      1305.19
02/05/2006      1313.21
03/05/2006      1307.85
04/05/2006      1312.25
05/05/2006      1325.76"
library(zoo)
z <- read.zoo(textConnection(Lines), format = "%d/%m/%Y")
rng <- range(time(z))
zz <- na.locf(merge(z, zoo(, seq(rng[1], rng[2], by = "day"))))
zz[format(time(zz), "%d") == "05"]



More information about the R-SIG-Finance mailing list