[R] Count number of Fridays in a month

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Oct 10 14:56:19 CEST 2014


Or try this:

fridays <- function(the_day){
    require(lubridate)
    day(the_day) = 1
    the_month = seq(the_day, the_day+months(1)-days(1),1)
    sum(wday(the_month) == 6)
}

That returns the number of Fridays in the month of a Date object given as arg:

> fridays(as.Date("2012-01-01"))
[1] 4
> fridays(as.Date("2012-02-01"))
[1] 4
> fridays(as.Date("2012-03-01"))
[1] 5
> fridays(as.Date("2012-04-01"))
[1] 4

Then you can build a function to compute the friday count ratio
between the month of a date and the previous month, given a data in
that string format you specified (d-m-y)

friday_ratio<- function(dayX){
    require(lubridate)
    the_day = as.Date(dmy(dayX))
    day(the_day)=1
    the_prev_month = the_day
    month(the_prev_month) = month(the_day)-1
    nf_current = fridays(the_day)
    nf_prev = fridays(the_prev_month)
    nf_current/nf_prev
}

> friday_ratio("31-may-2014")
[1] 1.25
> friday_ratio("1-may-2014")
[1] 1.25
> friday_ratio("1-jun-2014")
[1] 0.8
> friday_ratio("1-jul-2014")
[1] 1
> friday_ratio("1-aug-2014")
[1] 1.25

it is left as an exercise to convert this to the +1/0/-1 value - I
think the abs function may help...

Also, an exercise is to vectorise this over a vector of string dates.




On Fri, Oct 10, 2014 at 1:16 PM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> On 10/10/2014 8:10 AM, Abhinaba Roy wrote:
>> Hi Duncan,
>>
>> I have converted the string to a POSIXIt object using
>>
>> > strptime('31-may-2014',format="%d-%b-%Y")
>>
>> But could not figure out the way forward.
>>
>> Could you please elaborate a bit?
>
> Try this:
>
> ?POSIXlt
>
> Duncan Murdoch
>
>>
>> On Fri, Oct 10, 2014 at 5:14 PM, Duncan Murdoch
>> <murdoch.duncan at gmail.com <mailto:murdoch.duncan at gmail.com>> wrote:
>>
>>     On 10/10/2014, 7:28 AM, Abhinaba Roy wrote:
>>     > Hi R helpers,
>>     >
>>     > I want to write a function which will
>>     >
>>     > 1. Count the number of fridays in the current month ( to extract
>>     month from
>>     > given date) and also the number of fridays in the preceeding month
>>     >
>>     > 2. Calculate the ratio of the number of fridays in current month
>>     to the
>>     > number of fridays in the precceding month
>>     >
>>     > 3. Return a integer value calculated as
>>     >     ifelse(ratio>1,1,ifesle(ration<1,-1),0)
>>     >
>>     > The date which is passed is in the format *'31-may-2014'*
>>     >
>>     > So, given the date '31-may-2014'
>>     >
>>     > Number of fridays in May2014 = 5
>>     > Number of fridays in Apr2014 = 4
>>     >
>>     > Ratio = 5/4 >1
>>     > Hence, the function will return a value 1
>>     >
>>     > I want to call the function by passing '31-may-2014' as an argument
>>     >
>>     > How can this be done in R?
>>     >
>>     > Any help will be appreciated
>>
>>     Convert your string to a POSIXlt object using as.POSIXlt.  Then
>>     you can
>>     extract year, month and weekday from the result, and go from there.
>>     (The only unobvious part is figuring out how many days are in each
>>     month, but there are questions online giving various ways to do this.)
>>
>>     Duncan Murdoch
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list