Converting R Factors into Binary Matrix Values

前端 未结 3 409
猫巷女王i
猫巷女王i 2021-02-01 09:02

I would like to convert my dataframe into a matrix that expands a single factor column into multiple ones and assigns a 1/0 depending on the factor. Fo

3条回答
  •  一向
    一向 (楼主)
    2021-02-01 09:32

    Let's call your data.frame df:

    library(reshape2)
    dcast(df,C2*C3~C1,fill=0,length)
    
      C2 C3 A B
    1  1  1 1 0
    2  3  4 0 1
    3  3  5 1 0
    

提交回复
热议问题