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]]
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 appreciatedConvert 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
On Fri, Oct 10, 2014 at 7:28 AM, Abhinaba Roy <abhinabaroy09 at gmail.com> 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 argumentIf 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