[R] Count number of Fridays in a month

PO SU rhelpmaillist at 163.com
Sat Oct 11 04:55:32 CEST 2014


In my Impression, there seems  exsits a function (let just call weekday) which can return a POSIXlt format date 's weekday. That means,  weekday( 31-may-2014 ) may return the right weekday maybe 5, so  a clumsy method is start to judge from 1-may-2014 to 31-may-2014 , you get  a vector x, sum(which(x==5)) should return the right number. so do the April-2014. 
FOR MORE EFFICIENT, may be you should look into the function weekday ( not the true name) to get how it work ,it may help you do less things to get what you want.
FOR weekday,sorry for my forgot of where it located, it may in ?POSIXlt  ?format  ? strptime and other related keywords's document.





--

PO SU
mail: desolator88 at 163.com 
Majored in Statistics from SJTU




At 2014-10-10 19:44:17, "Duncan Murdoch" <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