You can bring together base R's reshape
and melt
from "reshape2" and have them work together instead of competing for attention as they normally do. To do so, you'll need to first add an "ID" variable (that is, if I understood your desired output correctly).
With your sample data, here's the approach I took:
MyData$ID <- sequence(nrow(MyData))
out <- melt(reshape(
MyData, direction = "long", idvar="ID", timevar = "Group",
varying = setdiff(names(MyData), "ID"), sep = "."),
id.vars = c("ID", "Group", "Time"))
And here's what it looks like. You'll have to reorder the result if you are looking for the specific order you described.
head(out)
# ID Group Time variable value
# 1 1 A 1 Z -0.10612452
# 2 2 A 2 Z 1.51152200
# 3 3 A 3 Z -0.09465904
# 4 4 A 4 Z 2.01842371
# 5 5 A 5 Z -0.06271410
# 6 6 A 6 Z 1.30486965
tail(out)
# ID Group Time variable value
# 35 5 B 5.141746 Y 0.03612261
# 36 6 B 6.019096 Y 0.20599860
# 37 7 B 7.236588 Y -0.36105730
# 38 8 B 7.634667 Y 0.75816324
# 39 9 B 9.156992 Y -0.72670483
# 40 10 B 10.205065 Y -1.36828104