0

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 ...
jogo
  • 12,469
  • 11
  • 37
  • 42
Elen
  • 3
  • 4

2 Answers2

0

You can try the filter function from dplyr.

filter(Data, data$Date< 2008 )

It would be great if you could provide the reproducible code.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
Sovik Gupta
  • 147
  • 2
  • 13
  • I've tried the code and i got this warning: Warning message: In Ops.factor(data$Date, 2008) : '<' not meaningful for factors . My data set is like this: head(data) Date LB CAC40 DAX DOW EURUSD BRENT 1 2/1/2004 86.71 3730.36 4018.16 10588.22 1.2534 32.22 2 3/1/2004 82.86 3625.22 3856.70 10354.96 1.2301 32.77 3 4/1/2004 73.39 3677.77 3978.26 10233.80 1.1984 34.48 4 5/1/2004 75.65 3671.49 3921.49 10203.79 1.2181 36.61 5 6/1/2004 75.25 3732.50 4065.40 10437.00 1.2200 34.48 6 7/1/2004 70.10 3654.40 3891.20 10138.70 1.2014 40.02 – Elen Apr 20 '18 at 11:04
  • this is not correct syntax. use `filter(Data, Date< 2008 )` instead – Roman Apr 20 '18 at 11:18
  • Syntax is fine you can use either of the one. I mentioned it so that its easy to understand that date is a column in the data which he/she is refering.Both are right – Sovik Gupta Apr 20 '18 at 11:21
0

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

JFraper
  • 46
  • 3