6

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 want to take this long format:

ID     condition    Value
John   a            2
John   a            3
John   b            4
John   b            5
John   a            6
John   a            2
John   b            1
John   b            4

To this wide-format without any aggregation:

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

I suppose that this is assuming that observations are paired and you were missing value would mess this up but any insight is appreciated

aleph4
  • 708
  • 1
  • 8
  • 15
  • 2
    Two things: 1. your wide-format does not match the long format data since `Alex` does not appear in the long format. 2. How do you know that `a==2` and `b==4` go together instead of `a==2` and `b==5` or some other combination of the values? I'm guessing you are not showing a column that indicates an experiment or some other identifier... – Chase May 11 '13 at 22:26
  • @Chase my guess is that they just have two rows for each ID*condition combination. But more info from aleph4 wouldn't hurt... – Dason May 11 '13 at 22:36

1 Answers1

9

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.)

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341