Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:12:56+00:00 2026-06-14T14:12:56+00:00

I am trying to write a function which takes a vector of dates as

  • 0

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)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:12:57+00:00Added an answer on June 14, 2026 at 2:12 pm

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that takes multiple arguments, which can come either
in ocaml I am trying to write a function which takes as an argument
I am trying to write a function in C++ which takes two 64 bit
I trying to write a generic function which takes the function and number of
I'm currently trying to write a function which takes an STL array of any
I was trying to write a function which takes the Id of an element
i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I'm trying to make it so I can write a function which defines a
I've had problem when trying write a function which has a default value when
I'm trying to write a program in which a program continuously takes in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.