-1

How to generate random start and end dates with particular duration in R.

For example I want to generate a start and end date (as separate variables) of 250 days between a period of 2000-01-01 and current date 2016-04-26

example result for 250 days
start=2014-03-01
end=2014-11-06

is there a inbuilt function available in R?

Edit

this is the comment I have given to jasper

I thought about that method but lets say programme generates starting date as 2016-04-23 but the end date will be 2016-12-29. I want dates between 2000-01-01 and current date 2016-04-26

Eka
  • 14,170
  • 38
  • 128
  • 212

2 Answers2

2

We can use seq

sample(seq(as.Date("2014-03-01"), length.out=251, by = "1 day"))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Why don't you just generate a start date somewhere between 2000-01-01 and 250 days before 2016-04-26, and then add 250 days to the start date?

Jasper
  • 555
  • 2
  • 12
  • I thought about that method but lets say programme generates starting date as 2016-04-23 but the end date will be 2016-12-29. I want dates between 2000-01-01 and current date 2016-04-26 – Eka Apr 27 '16 at 06:44
  • 1
    Then make sure the start date is between `2000-01-01` and `as.Date("2016-04-26") - 250`. That way the maximum end date will be `as.Date("2016-04-26") - 250 + 250` = `2016-04-26`. – Jasper Apr 27 '16 at 07:00