ricardo
ricardo

Reputation: 8435

Find first Tuesday of Month

I am trying to write a function which takes a vector of dates as an input and returns a vector of dates -- where the output is the date of the first Tuesday of the month which matches the input date.

So 2012-11-19 --> 2012-11-06, etc.

I have had some success with a single date, but have not been able to generalise to the vector case. Could someone please help?

This is what I have so far:

firstTuesday <- function(tt){
  ct <- as.POSIXct(tt)
  lt <- as.POSIXlt(tt)
  firstOf <- as.POSIXlt(ct - 60*60*24* (lt$mday - 1))
  if (firstOf$wday > 2) 
  {
    adjDays <- (9 - firstOf$wday)
    firstTues <- as.POSIXlt(as.POSIXct(firstOf) + 60*60*24*adjDays)
  }
  else {
    adjDays  <- (2 - firstOf$wday)
    firstTues <- as.POSIXlt(as.POSIXct(firstOf) + 60*60*24*adjDays)
  }
  return(firstTues)
}

Which works for a single date: firstTuesday(Sys.Date()) but yielded junk for vectors of dates (due to issues with if not being a vectorised control operator, i think).


I got around my limited understanding by using indexing. The following code seems to do the trick.

firstTuesday <- function(tt){
  ct <- as.POSIXct(tt)
  lt <- as.POSIXlt(tt)
  firstOf <- as.POSIXlt(ct - 60*60*24* (lt$mday - 1))
  firstTue <- as.POSIXct(firstOf)
  idx <- firstOf$wday > 2
  firstTue[idx]  <- as.POSIXct(firstOf[idx]) + 60*60*24*(9 - firstOf$wday[idx])
  firstTue[!idx]  <- as.POSIXct(firstOf[!idx]) + 60*60*24*(2 - firstOf$wday[!idx])
  return(firstTue)
}

Upvotes: 4

Views: 2138

Answers (2)

Joshua Ulrich
Joshua Ulrich

Reputation: 176698

Here's a simple solution using base functions:

firstDayOfMonth <- function(dates, day="Mon", abbreviate=TRUE) {
  # first 7 days of month
  s <- lapply(as.Date(format(dates,"%Y-%m-01")), seq, by="day", length.out=7)
  # first day of month
  d <- lapply(s, function(d) d[weekdays(d,abbreviate)==day])
  # unlist converts to atomic, so use do.call(c,...) instead
  do.call(c, d)
}

Well, maybe the do.call at the end isn't so simple... but it's a handy piece of knowledge. :)

R> d <- as.Date(c("2012-11-19","2012-11-19","2011-01-15"))
R> firstDayOfMonth(d, "Tuesday", FALSE)
[1] "2012-11-06" "2012-11-06" "2011-01-04"

Upvotes: 3

Rob Richmond
Rob Richmond

Reputation: 875

This uses lubridate and makes the logic a little simpler. Given a vector of dates the second function will return a vector of characters, similar to your input. You can change things around to suit your needs.

library(lubridate)

getTuesday = function(x) {
    date = ymd(x)
    first = floor_date(date,"month")
    dow = sapply(seq(0,6),function(x) wday(first+days(x)))
    firstTuesday = first + days(which(dow==3)-1)
    return(firstTuesday)
}

getMultipleTuesdays = function(y) {
    tmp = lapply(y, getTuesday)
    tmp = lapply(tmp, as.character)
    return(unlist(tmp))
}

Edit

Sample input/output

getMultipleTuesdays(c("2012-11-19","2012-11-19","2011-01-15"))
[1] "2012-11-06" "2012-11-06" "2011-01-04"

Upvotes: 4

Related Questions