(sorry if the title is not very informative: I don’t know how to define better this question)
I have my data in the following form:
In each group I have one pre value and one or two post values. I would like to convert this table to the following:

I was thinking to group the data with something like:
aggregate(mydata, by = group, FUN = myfunction)
or
ddply(mydata, .(group), .fun = myfunction)
and process the elements of each group in my function. But I don’t see how to do this because I need to pass both type and value to my function simultaneously. Is there a better way to do this?
Update: quick-and-dirty sample dataset:
mydata <- data.frame(group = sample(letters[1:5], 10, replace = TRUE),
type = sample(c("pre", "post"), 10, replace = TRUE),
value = rnorm(10))
Try something like this:
Or:
The key, in both solutions, is to create a “time” variable that is represented by the combination of “type” and a sequence variable that can be created with
ave.For completeness, here’s
dcastfrom “reshape2”: