Skip to content
Prev 5516 / 15274 Next

5th of month working day

Here is a second example that does the same thing except (that is it
returns the last value on or prior to the 5th); however, in the prior
solution the date was shown as the 5th and in this one it is shown as
the date of the last filled in value.  Not sure which you would
prefer.  See ?na.locf for more info on moving values forward into NAs
and also read the three vignettes that come with zoo.

# same as your example except I have removed the 5ths of the month and
added 4/4/2006.

Lines <- "
04/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
06/05/2006      1325.76"

        library(zoo)
	z <- read.zoo(textConnection(Lines), format = "%d/%m/%Y")

# z.na is same as z but with missing days added using NAs
# Its formed by merging z with a zoo-width series containing all days.

	rng <- range(time(z))
	z.na <- merge(z, zoo(, seq(rng[1], rng[2], by = "day")))

# form a series that has NAs wherever z.na does but has 1, 2, 3, ...
# instead of z.na's data values and then use na.locf to fill in NAs

	idx <- na.locf(seq_along(z.na) + (0 * z.na))

# pick off elements of z.na corresponding to 5th of month

	z.na[idx[format(time(z.na), "%d") == "05"]]


Here is the final result:

2006-04-04 2006-05-04
   1311.56    1312.25


On Mon, Jan 18, 2010 at 10:16 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote: