I have trouble generating the following dummy-variables in R:

I'm analyzing yearly data time series data for the period 1948-2009 I have two questions

  1. How do I generate a dummy variable for observation #10, i.e. for year 1957 (value = 1 at 1957 and zero otherwise)?

  2. How do i generate a dummy variable which is zero before 1957 and takes the value 1 from 1957 and onwards to 2009?

Best Answer


Another option that can work better if you have many variables is factor and model.matrix .

> year.f = factor(year)
> dummies = model.matrix(~year.f)

This will include an intercept column (all ones) and one column for each of the years in your data set except one, which will be the "default" or intercept value.

You can change how the "default" is chosen by messing with contrasts.arg in model.matrix .

Also, if you want to omit the intercept, you can just drop the first column or add +0 to the end of the formula.

I hope you find it useful