> df <- data.frame(g=c(1,2,3,1,2,3), foo_x=c(12,32,14,23,12,34), foo_y=c(32,35,12,12,32,34), bar_x=c(100,233,122,433,122,442), bar_y=c(744,546,236,137,652,235))
I'm trying to create this data frame:
g foo bar val_foo val_bar
1 x x 12 100
2 x x 32 233
3 x x 14 122
1 x y 32 744
2 x y 35 546
3 x y 12 236
1 y x 23 433
2 y x 12 122
3 y x 34 442
1 y y 12 137
2 y y 32 652
3 y y 34 235
I think I should be using the melt
function, but how can I create two different variable (foo
and bar
) and value (val_foo
and val_bar
) columns? melt(df, id.vars="g")
only creates a single variable/value pair.