Generate a dummy-variable

前端 未结 17 973
遇见更好的自我
遇见更好的自我 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:16

    What I normally do to work with this kind of dummy variables is:

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

    data$factor_year_1 <- factor ( with ( data, ifelse ( ( year == 1957 ), 1 , 0 ) ) )
    

    (2) how do I generate a dummy-variable which is zero before 1957 and takes the value 1 from 1957 and onwards to 2009?

    data$factor_year_2 <- factor ( with ( data, ifelse ( ( year < 1957 ), 0 , 1 ) ) )
    

    Then, I can introduce this factor as a dummy variable in my models. For example, to see whether there is a long-term trend in a varible y :

    summary ( lm ( y ~ t,  data = data ) )
    

    Hope this helps!

提交回复
热议问题