Re: [R] Count number of Fridays

2014-10-10 Thread Abhinaba Roy
Thanks Jim :) Regards Abhinaba On Fri, Oct 10, 2014 at 9:02 PM, jim holtman wrote: > Here is one way of doing it: > > > > require(lubridate) > > now <- as.Date('2014-10-10') # some date > > # get first of month > > first_mon <- now - day(now) + 1 > > # create sequence of days in the month so y

Re: [R] Count number of Fridays in a month

2014-10-10 Thread PO SU
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(w

Re: [R] Count number of Fridays in a month

2014-10-10 Thread Gabor Grothendieck
On Fri, Oct 10, 2014 at 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 numb

Re: [R] Count number of Fridays

2014-10-10 Thread jim holtman
Here is one way of doing it: > require(lubridate) > now <- as.Date('2014-10-10') # some date > # get first of month > first_mon <- now - day(now) + 1 > # create sequence of days in the month so you can count Fridays > x <- seq(first_mon, by = '1 day', length = days_in_month(first_mon)) > first_f

Re: [R] Count number of Fridays in a month

2014-10-10 Thread Barry Rowlingson
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 >

Re: [R] Count number of Fridays in a month

2014-10-10 Thread Duncan Murdoch
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

Re: [R] Count number of Fridays in a month

2014-10-10 Thread 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? On Fri, Oct 10, 2014 at 5:14 PM, Duncan Murdoch wrote: > On 10/10/2014, 7:28 AM, Abhinaba Roy wrote: > > Hi

Re: [R] Count number of Fridays in a month

2014-10-10 Thread Duncan Murdoch
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 f