Generate a dummy-variable

前端 未结 17 974
遇见更好的自我
遇见更好的自我 2020-11-21 11:41

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

I\'m analyzing yearly time series data (time period 1948-2009). I have two questions:

  1. <
17条回答
  •  有刺的猬
    2020-11-21 12:19

    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.

提交回复
热议问题