0

I want to create a new dummy variable that prints 1 if my observation is within a certain set of date ranges, and a 0 if its not. My dataset is a list of political contributions over a 10 year range and I want to make a dummy variable to mark if the donation came during a certain range of dates. I have 10 date ranges I'm looking at.

Does anyone know if the right way to do this is to create a loop? I've been looking at this question, which seems similar, but I think mine would be a bit more complicated: Creating a weekend dummy variable

By way of example, what I have a variable listing dates that contributions were recorded and I want to create dummy to show whether this contribution came during a budget crisis. So, if there were a budget crisis from 2010-2-01 until 2010-03-25 and another from 2009-06-05 until 2009-07-30, the variable would ideally look like this:

Contribution Date.......Budget Crisis
2009-06-01...........................0
2009-06-06...........................1
2009-07-30...........................1
2009-07-31...........................0
2010-01-31...........................0
2010-03-05...........................1
2010-03-26...........................0

Thanks yet again for your help!

Community
  • 1
  • 1
ModalBro
  • 544
  • 5
  • 25
  • 2
    Please share a small sample of dates, the form of your conditions, and a sample of your expected output. – A5C1D2H2I1M1N2O1R2T1 Feb 23 '14 at 05:13
  • Something like this? http://stackoverflow.com/questions/9500114/find-which-season-a-particular-date-belongs-to – Roman Luštrik Feb 23 '14 at 08:07
  • Insufficient information. While your question seems simple, there may be hidden complexity in your choice of data types, etc. Please provide at least a small example of the data you are using. Is it a data frame? – Alex Brown Feb 23 '14 at 08:20
  • Thanks Roman for the link. I think that example is on the right track. I added an example to hopefully make things clearer. – ModalBro Feb 24 '14 at 02:45

1 Answers1

1

This looks like a good opportunity to use the %in% syntax of the match(...) function.

dat <- data.frame(ContributionDate = as.Date(c("2009-06-01", "2009-06-06", "2009-07-30", "2009-07-31", "2010-01-31", "2010-03-05", "2010-03-26")), CrisisYes = NA)

crisisDates <- c(seq(as.Date("2010-02-01"), as.Date("2010-03-25"), by = "1 day"),
                 seq(as.Date("2009-06-05"), as.Date("2009-07-30"), by = "1 day")
                 )

dat$CrisisYes <- as.numeric(dat$ContributionDate %in% crisisDates)
dat

 ContributionDate CrisisYes
1       2009-06-01         0
2       2009-06-06         1
3       2009-07-30         1
4       2009-07-31         0
5       2010-01-31         0
6       2010-03-05         1
7       2010-03-26         0
alex
  • 345
  • 2
  • 10