Very interesting question! I tried using runif()
, but didn't get it. In the end, this is what I did:
dates <- sample(seq(from = as.Date("2000-01-01"), to = as.Date("2002-12-31"), length.out = 1000), 100, replace = TRUE)
I put some configurations that are very arbitrary. What I did was: first, I set 1000 steps that uniformily go from January 1, 2000 to December 31, 2002. From those, I told the program to select 100 at random, with repeating allowed. These were arbitrary configurations.
Note that this isn't even truly pseudo-random like runif()
; they will be uniformily scattered across the line, but the larger the steps-to-selected ratio, the more it looks random.
EDIT: There's a chance I misunderstood your question. At first I thought you wanted to generate dummy date data in the range 2000-2002 (and later 2007-2009), but now I think you want to identify if it's inside the range... Either way, I don't really understand. If it's the second, this is the code:
df <- data.frame(Dates = sample(seq(from = as.Date("1998-01-01"), to = as.Date("2004-12-31"), length.out = 2000), 200, replace = TRUE),
otherStuff = rnorm(200))
df$isInRange <- ifelse(df$Dates > as.Date("2000-01-01") & df$Dates < as.Date("2002-12-31"), 1, 0)
That is, if you don't want to use anything but base R.