Discussion:
[R] Count number of Fridays in a month
Abhinaba Roy
2014-10-10 11:28:12 UTC
Permalink
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

Thanks and regards,
Abhinaba

[[alternative HTML version deleted]]
Duncan Murdoch
2014-10-10 11:44:17 UTC
Permalink
Post by Abhinaba Roy
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
Abhinaba Roy
2014-10-10 12:10:02 UTC
Permalink
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?

On Fri, Oct 10, 2014 at 5:14 PM, Duncan Murdoch <murdoch.duncan at gmail.com>
Post by Abhinaba Roy
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
Post by Abhinaba Roy
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
[[alternative HTML version deleted]]
Duncan Murdoch
2014-10-10 12:16:09 UTC
Permalink
Post by Abhinaba Roy
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
Post by Abhinaba Roy
On Fri, Oct 10, 2014 at 5:14 PM, Duncan Murdoch
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
Barry Rowlingson
2014-10-10 12:56:19 UTC
Permalink
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)
}
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
Post by Abhinaba Roy
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?
?POSIXlt
Duncan Murdoch
Post by Abhinaba Roy
On Fri, Oct 10, 2014 at 5:14 PM, Duncan Murdoch
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.
PO SU
2014-10-11 02:55:32 UTC
Permalink
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
Post by Duncan Murdoch
Post by Abhinaba Roy
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.
Gabor Grothendieck
2014-10-10 23:00:39 UTC
Permalink
Post by Abhinaba Roy
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
If d is a "Date" class variable equal to a month end date, e.g.

d <- as.Date("31-may-2014", format = "%d-%b-%Y")

then this gives the ratio of the number of Fridays in d's month
to the number of Fridays in the prior month:

days <- seq(as.Date(cut(d - 32, "month")), d, "day")
ratio <- exp(diff(log(tapply(format(days, "%w") == 5, format(days,
"%Y%m"), sum))))

Now apply the formula in your point 3 to ratio and put it all in a function.
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
Continue reading on narkive:
Loading...