I’ve got a semi-long data frame in the following format:
| Id | Alt | Choice | Var1 | Var2 |
|----+-----+--------+------+------|
| 1 | a | TRUE | 58.2 | 4 |
| 1 | b | FALSE | 71.6 | 5 |
| 2 | a | FALSE | 14.8 | 2 |
| 2 | b | TRUE | 82.1 | 5 |
…that I’d like to reshape into the following format:
| Id | Choice.a | Choice.b | Var1.a | Var1.b | Var2.a | Var2.b |
|----+----------+----------+--------+--------+--------+--------|
| 1 | TRUE | FALSE | 58.2 | 71.6 | 4 | 5 |
| 2 | FALSE | TRUE | 14.8 | 82.1 | 2 | 5 |
I keep thinking to myself that I ought to be able to do something like:
library(reshape2)
dcast(df, ... ~ Alt)
But that’s not quite right.
This is not using ‘plyr’ but rather the base function
reshape:If you need the precise order you showed, it’s easiy enough to use “[“. Imagine we assigned the result of reshape() to “wdat”: