Reputation: 23114
For example:
require(reshape2)
var1 = c(rep('john', 3), rep('aron', 2), 'frank')
var2 = c('john', 'aron', 'frank', 'aron', 'frank', 'frank')
var3 = rnorm(6)
mydat = data.frame(var1, var2, var3)
acast(mydat, var1~var2)
Result:
Using var3 as value column: use value.var to override.
aron frank john
aron 0.32026 0.22858 NA
frank NA 0.57658 NA
john 0.30543 1.16453 -2.0021
This messes up the original order, ideally one would like to see the 3 NAs nicely in the lower triangle.
Upvotes: 3
Views: 971
Reputation: 193547
Generally, when questions come up about "how do I preserve the order of...", I try factor
as one of the first solutions and specify the desired order of my factor
s:
mydat$var1 <- factor(mydat$var1, c("john", "aron", "frank"))
mydat$var2 <- factor(mydat$var2, c("john", "aron", "frank"))
acast(mydat, var1~var2)
# Using var3 as value column: use value.var to override.
# john aron frank
# john 0.464706 1.77877633 0.5925874
# aron NA 0.04940059 -0.3180871
# frank NA NA -1.3888493
Upvotes: 3