Reputation: 1
We have daily exchange rate data for 23 years between 1990 - 2013, for two countries. We wish to create a dummy variable for a particular time period equal to 2000-2002, so when the time is between 2000 and 2002 its equal to 1, and every other year, its equal to 0. We would also create another dummy variable for 2007 - 2009 where, like the previous question, is equal to 0 when the time is not between our dates, and equal to 1 when it is.
How do we go about this? Please help us.
Upvotes: 0
Views: 575
Reputation: 1015
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.
Upvotes: 1