How to use “cast” in reshape without aggregation

后端 未结 1 614
甜味超标
甜味超标 2020-12-17 02:18

In many uses of cast I\'ve seen, an aggregation function such as mean is used.

How about if you simply want to reshape without information loss. For example, if I wa

相关标签:
1条回答
  • 2020-12-17 02:38

    In such cases you can add a sequence number:

    library(reshape2)
    
    DF$seq <- with(DF, ave(Value, ID, condition, FUN = seq_along))
    dcast(ID + seq ~ condition, data = DF, value.var = "Value")
    

    The last line gives:

        ID seq a b
    1 John   1 2 4
    2 John   2 3 5
    3 John   3 6 1
    4 John   4 2 4
    

    (Note that we used the sample input from the question but the sample output in the question does not correspond to the sample input.)

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