3

I would like to generate a dataframe that contains all the Friday dates for the whole year. Is there a simple way to do this?

eg for December 2013: (6/12/13,13/12/13,20/12/13,27/12/13)

Thank you for your help.

adam.888
  • 7,686
  • 17
  • 70
  • 105

5 Answers5

7

I'm sure there is a simpler way, but you could brute force it easy enough:

dates <- seq.Date(as.Date("2013-01-01"),as.Date("2013-12-31"),by="1 day")

dates[weekdays(dates)=="Friday"]
dates[format(dates,"%w")==5]

Building on @Frank's good work, you can find all of any specific weekday between two dates like so:

pick.wkday <- function(selday,start,end) {
  fwd.7 <- start + 0:6
  first.day <- fwd.7[as.numeric(format(fwd.7,"%w"))==selday]
  seq.Date(first.day,end,by="week")
}

start and end need to be Date objects, and selday is the day of the week you want (0-6 representing Sunday-Saturday).

i.e. - for the current query:

pick.wkday(5,as.Date("2013-01-01"),as.Date("2013-12-31"))
thelatemail
  • 91,185
  • 12
  • 128
  • 188
5

Here is a way.

d <- as.Date(1:365, origin = "2013-1-1")
d[strftime(d,"%A") == "Friday"]

Alternately, this would be a more efficient approach for generating the data for an arbitrary number of Fridays:

wk1 <- as.Date(seq(1:7), origin = "2013-1-1") # choose start date & make 7 consecutive days
wk1[weekdays(wk1) == "Friday"]                # find Friday in the sequence of 7 days

seq.Date(wk1[weekdays(wk1) == "Friday"], length.out=50, by=7) # use it to generate fridays

by=7 says go to the next Friday. length.out controls the number of Fridays to generate. One could also use to to control how many Fridays are generated (e.g. use to=as.Date("2013-12-31") instead of length.out).

Jota
  • 17,281
  • 7
  • 63
  • 93
3

Takes a year as input and returns only the fridays...

getFridays <- function(year) {      
  dates <- seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")), by = "day")
  dates[weekdays(dates) == "Friday"]      
}

Example:

> getFridays(2000)
 [1] "2000-01-07" "2000-01-14" "2000-01-21" "2000-01-28" "2000-02-04" "2000-02-11" "2000-02-18" "2000-02-25" "2000-03-03" "2000-03-10" "2000-03-17" "2000-03-24" "2000-03-31"
[14] "2000-04-07" "2000-04-14" "2000-04-21" "2000-04-28" "2000-05-05" "2000-05-12" "2000-05-19" "2000-05-26" "2000-06-02" "2000-06-09" "2000-06-16" "2000-06-23" "2000-06-30"
[27] "2000-07-07" "2000-07-14" "2000-07-21" "2000-07-28" "2000-08-04" "2000-08-11" "2000-08-18" "2000-08-25" "2000-09-01" "2000-09-08" "2000-09-15" "2000-09-22" "2000-09-29"
[40] "2000-10-06" "2000-10-13" "2000-10-20" "2000-10-27" "2000-11-03" "2000-11-10" "2000-11-17" "2000-11-24" "2000-12-01" "2000-12-08" "2000-12-15" "2000-12-22" "2000-12-29"
Tommy O'Dell
  • 7,019
  • 13
  • 56
  • 69
2

There are probably more elegant ways to do this, but here's one way to generate a vector of Fridays, given any year.

year = 2007
st <- as.POSIXlt(paste0(year, "/1/01"))
en <- as.Date(paste0(year, "/12/31"))

#get to the next Friday
skip_ahead <- 5 - st$wday
if(st$wday == 6) skip_ahead <- 6  #for Saturdays, skip 6 days ahead.

first.friday <- as.Date(st) + skip_ahead
dates <- seq(first.friday, to=en, by ="7 days")

dates
#[1] "2007-01-05" "2007-01-12" "2007-01-19" "2007-01-26"
# [5] "2007-02-02" "2007-02-09" "2007-02-16" "2007-02-23"
# [9] "2007-03-02" "2007-03-09" "2007-03-16" "2007-03-23"
Ram Narasimhan
  • 22,341
  • 5
  • 49
  • 55
2

I think this would be the most efficient way and would also returns all the Friday in the whole of 2013.

FirstWeek <-   seq(as.Date("2013/1/1"), as.Date("2013/1/7"), "days")
seq(
  FirstWeek[weekdays(FirstWeek) == "Friday"],
  as.Date("2013/12/31"),
  by = "week"
)
TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54