R: Expanding an R factor into dummy columns for every factor level

前端 未结 1 462
独厮守ぢ
独厮守ぢ 2021-01-15 02:19

I have a quite big data frame in R with two columns. I am trying to make out of the Code column (factor type with 858 levels) the dummy variables.

1条回答
  •  被撕碎了的回忆
    2021-01-15 02:55

    This worked for me perfectly:

    library(reshape2)
    m <- acast(data = d, User ~ Code)
    

    The only thing was that it produced NAs, instead of 0s, but this can be easily changed with this:

    m[is.na(m)] <- 0
    

    0 讨论(0)
提交回复
热议问题