Pivoting rows into columns

后端 未结 4 1579
我寻月下人不归
我寻月下人不归 2021-02-05 15:57

Suppose (to simplify) I have a table containing some control vs. treatment data:

Which, Color, Response, Count
Control, Red, 2, 10
Control, Blue, 3, 20
Treatment         


        
4条回答
  •  一个人的身影
    2021-02-05 16:28

    Using the reshape package.

    First, melt your data.frame:

    x <- melt(df) 
    

    Then cast:

    dcast(x, Color ~ Which + variable)
    

    Depending on which version of the reshape package you're working with it could be cast() (reshape) or dcast() (reshape2)

    Voila.

提交回复
热议问题