[R] Count number of Fridays

jim holtman jholtman at gmail.com
Fri Oct 10 17:32:32 CEST 2014


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_fri <- sum(wday(x) == 6)  # count fridays
> # first of previous month
> prev_mon <- first_mon - day(first_mon - 1)
> # create sequence of days in month
> x <- seq(prev_mon, by = '1 day', length = days_in_month(prev_mon))
> prev_fri <- sum(wday(x) == 6)
>
> cat('Fri this month:', first_fri, 'Fri last month:', prev_fri, '\n')
Fri this month: 5 Fri last month: 4
> sign(first_fri - prev_fri)  # will do the 'ifelse'-type test you want
[1] 1
>

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Fri, Oct 10, 2014 at 7:31 AM, Abhinaba Roy <abhinabaroy09 at gmail.com> wrote:
> Hi Jim,
>
> Thanks for your previous solution. I am learning to work with dates now.
>
> 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
>
> On Mon, Aug 4, 2014 at 9:36 PM, jim holtman <jholtman at gmail.com> wrote:
>>
>> Here is the solution using 'rle':
>>
>> > require(data.table)
>> > x <- read.table(text = "CASE_ID YEAR_MTH ATT_1
>> +  CB26A    201302         1
>> +  CB26A    201302         0
>> +  CB26A    201302         0
>> +  CB26A    201303         1
>> +  CB26A    201303         1
>> +  CB26A    201304         0
>> +  CB26A    201305         1
>> +  CB26A    201305         0
>> +  CB26A    201306         1
>> +  CB27A    201304         0
>> +  CB27A    201304         0
>> +  CB27A    201305         1
>> +  CB27A    201306         1
>> +  CB27A    201306         0
>> +  CB27A    201307         0
>> +  CB27A    201308         1", header = TRUE, as.is = TRUE)
>> > setDT(x)
>> > # convert to a Date object for comparison
>> > x[, MYD := as.Date(paste0(YEAR_MTH, '01'), format = "%Y%m%d")]
>> > # separate by CASE_ID and only keep the first 3 months
>> > x[
>> +      , {
>> +          # determine the end date as 3 months from the first date
>> +          endDate <- seq(MYD[1L], by = '3 months', length = 2)[2L]
>> +          # now count the changes
>> +          list(nChanges = length(rle(ATT_1[(MYD >= MYD[1L]) & (MYD <=
>> endDate)])[[1L]]) - 1L)
>> +        }
>> +      , by = CASE_ID
>> +      ]
>>    CASE_ID nChanges
>> 1:   CB26A        5
>> 2:   CB27A        2
>>
>> Jim Holtman
>> Data Munger Guru
>>
>> What is the problem that you are trying to solve?
>> Tell me what you want to do, not how you want to do it.
>>
>>
>> On Mon, Aug 4, 2014 at 11:39 AM, Bert Gunter <gunter.berton at gene.com>
>> wrote:
>> > Or ?rle
>> >
>> > Bert
>> >
>> >
>> >
>> > Sent from my iPhone -- please excuse typos.
>> >
>> >> On Aug 4, 2014, at 8:28 AM, jim holtman <jholtman at gmail.com> wrote:
>> >>
>> >> Try this, but I only get 2 changes for CB27A instead of you indicated
>> >> 3:
>> >>
>> >>> require(data.table)
>> >>> x <- read.table(text = "CASE_ID YEAR_MTH ATT_1
>> >> + CB26A    201302         1
>> >> + CB26A    201302         0
>> >> + CB26A    201302         0
>> >> + CB26A    201303         1
>> >> + CB26A    201303         1
>> >> + CB26A    201304         0
>> >> + CB26A    201305         1
>> >> + CB26A    201305         0
>> >> + CB26A    201306         1
>> >> + CB27A    201304         0
>> >> + CB27A    201304         0
>> >> + CB27A    201305         1
>> >> + CB27A    201306         1
>> >> + CB27A    201306         0
>> >> + CB27A    201307         0
>> >> + CB27A    201308         1", header = TRUE, as.is = TRUE)
>> >>> setDT(x)
>> >>> # convert to a Date object for comparison
>> >>> x[, MYD := as.Date(paste0(YEAR_MTH, '01'), format = "%Y%m%d")]
>> >>> # separate by CASE_ID and only keep the first 3 months
>> >>> x[
>> >> +     , {
>> >> +         # determine the end date as 3 months from the first date
>> >> +         endDate <- seq(MYD[1L], by = '3 months', length = 2)[2L]
>> >> +         # extract what is changing
>> >> +         changes <- ATT_1[(MYD >= MYD[1L]) & (MYD <= endDate)]
>> >> +         # now count the changes
>> >> +         list(nChanges = sum(head(changes, -1L) != tail(changes,
>> >> -1L)))
>> >> +       }
>> >> +     , by = CASE_ID
>> >> +     ]
>> >>   CASE_ID nChanges
>> >> 1:   CB26A        5
>> >> 2:   CB27A        2
>> >>
>> >> Jim Holtman
>> >> Data Munger Guru
>> >>
>> >> What is the problem that you are trying to solve?
>> >> Tell me what you want to do, not how you want to do it.
>> >>
>> >>
>> >>> On Wed, Jul 30, 2014 at 3:08 AM, Abhinaba Roy
>> >>> <abhinabaroy09 at gmail.com> wrote:
>> >>> Dear R-helpers,
>> >>>
>> >>> I want to count the number of times ATT_1 has changed in a period of 3
>> >>> months(can be 4months) from the first YEAR_MTH entry for a CASE_ID. So
>> >>> if
>> >>> for a CASE_ID we have data only for two distinct YEAR_MTH, then all
>> >>> the
>> >>> entries should be considered, otherwise only the relevant entries will
>> >>> be
>> >>> considered for calculation.
>> >>> E.g. if the first YEAR_MTH entry is 201304 then get the number of
>> >>> changes
>> >>> till 201307(inclusive), similarly if the first YEAR_MTH entry is
>> >>> 201302
>> >>> then get the number of changes till 201305.
>> >>>
>> >>> Dataset
>> >>> CASE_ID YEAR_MTH ATT_1
>> >>> CB26A    201302         1
>> >>> CB26A    201302         0
>> >>> CB26A    201302         0
>> >>> CB26A    201303         1
>> >>> CB26A    201303         1
>> >>> CB26A    201304         0
>> >>> CB26A    201305         1
>> >>> CB26A    201305         0
>> >>> CB26A    201306         1
>> >>> CB27A    201304         0
>> >>> CB27A    201304         0
>> >>> CB27A    201305         1
>> >>> CB27A    201306         1
>> >>> CB27A    201306         0
>> >>> CB27A    201307         0
>> >>> CB27A    201308         1
>> >>>
>> >>> The final dataset should look like
>> >>>
>> >>> ID_CASE    No.of changes
>> >>> CB26A        5
>> >>> CB27A        3
>> >>>
>> >>> where 'No.of changes' refer to the change in 3 months (201302-201305
>> >>> for
>> >>> CB26A and 201304-201307 for CB27A).
>> >>>
>> >>> How can this be done in R?
>> >>>
>> >>> Regards,
>> >>> Abhinaba Roy
>> >>>
>> >>>        [[alternative HTML version deleted]]
>> >>>
>> >>> ______________________________________________
>> >>> 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.
>> >>
>> >> ______________________________________________
>> >> 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