Retaining variables in dcast in R

前端 未结 2 811
旧时难觅i
旧时难觅i 2021-01-22 12:41

I am using the dcast function in R to turn a long-format dataset into a wide-format dataset. I have an ID number, a categorical variable (CAT

2条回答
  •  死守一世寂寞
    2021-01-22 13:21

    For that, you need to add SEX to the ID side of your formula:

    dcast(PC1, ID + SEX~CAT, value.var='AMT', fun.aggregate=sum, na.rm=TRUE)
    # results in:
    
      ID    SEX  A  B  C
    1  1 Female 46 22 31
    2  2   Male 17 25 44
    

    Things on the left hand side of the formula are kept as-is, things on the right-hand side are cast.

提交回复
热议问题