I have a question about group combinations.
My mini sample looks like this:
sample <- data.frame( group=c("a","a","a","a","b","b","b"), number=c(1,2,3,2,4,5,3) )
If I apply the combn function to a data frame, this gives me the following result, which is all combinations of values ββunder the number column, regardless of which group this value belongs to:
[,1] [,2] [1,] 1 2 [2,] 1 3 [3,] 1 2 [4,] 1 4 [5,] 1 5 [6,] 1 3 [7,] 2 3 [8,] 2 2 [9,] 2 4 [10,] 2 5 [11,] 2 3 [12,] 3 2 [13,] 3 4 [14,] 3 5 [15,] 3 3 [16,] 2 4 [17,] 2 5 [18,] 2 3 [19,] 4 5 [20,] 4 3 [21,] 5 3
The code I used for the above results is as follows:
t(combn((sample$number), 2))
However, I would like to get the results of the combination within the group (ie "a", "b"). Therefore, the result I want to get should look like this:
[,1] [,2] [,3] [1,] a 1 2 [2,] a 1 3 [3,] a 1 2 [4,] a 2 3 [5,] a 2 2 [6,] a 3 2 [7,] b 4 5 [8,] b 4 3 [9,] b 5 3
In addition to the combinations, I would like to get a column indicating the group.