Try something like this:
mydf <- data.frame(group = c("A", "A", "B", "B", "C", "C", "C", "D", "D", "E", "E"), type = c("pre", "post", "pre", "post", "pre", "post", "post", "pre", "post", "pre", "post"), value = 1:11) times <- with(mydf, ave(value, group, type, FUN = seq_along)) xtabs(value ~ group + interaction(type, times), mydf)
Or:
times <- with(mydf, ave(value, group, type, FUN = seq_along)) mydf$timevar <- interaction(mydf$type, times) reshape(mydf, direction = "wide", idvar = "group", timevar="timevar", drop="type")
The key in both solutions is to create a βtimeβ variable, which is represented by a combination of βtypeβ and a variable sequence that can be created using ave .
For completeness, here is the dcast from "reshape2":
times <- with(mydf, ave(value, group, type, FUN = seq_along)) library(reshape2) dcast(mydf, group ~ type + times)