Generate a dummy-variable

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

    Using dummies::dummy():

    library(dummies)
    
    # example data
    df1 <- data.frame(id = 1:4, year = 1991:1994)
    
    df1 <- cbind(df1, dummy(df1$year, sep = "_"))
    
    df1
    #   id year df1_1991 df1_1992 df1_1993 df1_1994
    # 1  1 1991        1        0        0        0
    # 2  2 1992        0        1        0        0
    # 3  3 1993        0        0        1        0
    # 4  4 1994        0        0        0        1
    

提交回复
热议问题