Search code examples
rvariablesdummy-variabledummy-data

Dummy variable for year in R


So my data set consists 7 variables and one of them is date from 2004 to 2008, and I want to create a dummy variable for the year of 2008. Specifically, I want to know what happened before 2008 and after and have levels 0 and 1, but I don't know how to manage this in R.

str(data)
'data.frame':   56 obs. of  8 variables:
 $ Date     : Factor w/ 56 levels "1/1/2005","1/1/2006",..: 17 22 27 32 37 42 47 52 5 9 ...
 $ LB       : num  86.7 82.9 73.4 75.7 75.2 ...
 $ CAC40    : num  3730 3625 3678 3671 3732 ...
 $ DAX      : num  4018 3857 3978 3921 4065 ...
 $ DOW      : num  10588 10355 10234 10204 10437 ...
 $ EURUSD   : num  1.25 1.23 1.2 1.22 1.22 ...
 $ BRENT    : num  32.2 32.8 34.5 36.6 34.5 ...

Solution

  • If your data is a dataframe, you could create a dummy variable doing

     data$dummyYear <- as.numeric(data$year < 2008)
    

    This new variable has the value 1 for years 2004:2007 and 0 for 2008