I have the following variables in a data frame:
[1] \"Type\" \"I.alt\" \"idx06\" \"idx07\" \"idx08\" \"farve1\" \"farve2\"
If I do:
This doesn't answer your question about cast, but you could certainly subset and do two melts, followed by a merge:
dm1 <- melt(d[,c("Type","I.alt","idx06","idx07","idx08")], id=c("Type","I.alt"))
dm2 <- melt(d[,c("Type","I.alt","farve1","farve2")], id=c("Type","I.alt"))
colnames(dm2) <- c("Type", "I.alt", "variable2", "value2")
dm <- merge(dm1, dm2)
Or, equivalently, do one melt (like you're currently doing) then subset the melted dataframe twice (idx <- variable %in% c("idx06","idx07","idx08"
) as one and !idx
as the other) and merge that output.
Either way you get what you want:
> head(dm)
Type I.alt variable value variable2 value2
1 Alvorligere vold 1154 idx08 1.108696 farve1 red
2 Alvorligere vold 1154 idx08 1.108696 farve2 red
3 Alvorligere vold 1154 idx07 1.027174 farve1 red
4 Alvorligere vold 1154 idx07 1.027174 farve2 red
5 Alvorligere vold 1154 idx06 1.000000 farve1 red
6 Alvorligere vold 1154 idx06 1.000000 farve2 red