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

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jan 19 15:21:28 CET 2010


The first line should have read

Here is a second example that does the same thing (that is it

On Tue, Jan 19, 2010 at 9:20 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> 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:
>> 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