Generate a dummy-variable

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.

Hope this is useful.

Leave a Comment