Let say I have below data.frame
library(reshape2)
set.seed(1)
dat = data.frame(X1 = sample(letters, 10, replace = T), X2 = sample(letters, 10, replace = T), X3 = sample(LETTERS, 10, replace = T), X4 = sample(LETTERS[1:4], 10, replace = T), X5 = sample(11:13, 10, replace = T), X6 = sample(200:201, 10, replace = T))
dat
X1 X2 X3 X4 X5 X6
1 q w J B 12 201
2 t c U A 12 200
3 q c W A 11 200
4 b u E C 11 201
5 p m C B 13 201
6 g t V A 12 201
7 t d F C 13 201
8 x t P D 11 201
9 d e E D 13 200
10 l m L D 13 201
Now I want to un-melt
above data frame such that, the column will be the unique combination of the values of columns X4 & X5
, value vector will be X6
, which will corresponds to all rows except X4, X5, and X6
(my actual dataframe has many columns so I can not explicitly name those remaining columns)
So in above case, the columns of the final dataframe will be {X1, X2, X3, A-12 A-13, ... B-12, B-13, .. etc}
Can you please help me how to achieve this with dcast()
function from reshape
?
Thanks for your pointer.