I have this data.frame in R:
df = data.frame("blue" = c(0,1,1,0,1),
"yellow" = c(0,0,1,0,1),
"green" = c(1,1,1,0,0),
"letter" = c("A","B","C","D","E"),
"id" = c(23,57,48,3,12))
blue yellow green Letter ID
1 0 0 1 A 23
2 1 0 1 B 57
3 1 1 1 C 48
4 0 0 0 D 3
5 1 1 0 E 12
and would like to turn it into a data frame with all possible combinations of colors (colnames to rows), maintaining the Letter and ID of each pair, like in here:
Col_1 Col_2 C1 C2 Letter ID
1 blue yellow 0 0 A 23
2 blue green 0 1 A 23
3 yellow green 0 1 A 23
4 blue yellow 1 0 B 57
5 blue green 1 1 B 57
6 yellow green 0 1 B 57
7 blue yellow 1 1 C 48
8 blue green 1 1 C 48
9 yellow green 1 1 C 48
10 blue yellow 0 1 D 3
11 blue green 0 0 D 3
12 yellow green 1 0 D 3
13 blue yellow 1 1 E 12
14 blue green 1 0 E 12
15 yellow green 1 0 E 12
Since my database is huge doing so with loops is taking too long. Any suggestions to do it more efficiently?
Thank you.