Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combinations by group in R

Tags:

r

combinations

I have a question about combinations by group.

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 function of combnto the data frame,it gives me following result, which is all the combinations of the values under the 'number' column regardless of which group the 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 that I used for the results above is as follows:

t(combn((sample$number), 2))

However, I would like to get the combination results within the group (i.e., "a", "b"). Therefore, the result that 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 the column indicating the group.

like image 298
Emily Avatar asked Jul 06 '16 10:07

Emily


People also ask

How to generate combinations in R?

To generate combinations in R, we can use the conbn method which will generate all possible groups of k from n items. The signature of the method looks like cobn (items, k). If we would like to generate all combnations of groups of 3 from the numbers 1-4, we can do the following: Each column in the output is a distinct group.

What is the group by function in R?

The group by function is a very essential part of the dplyr package and a necessity for someone who uses R to work with data. For tasks that involve data cleaning and categorical analysis of data, the group by function almost always comes into play.

What is Combinat package in R language?

R language allows us the ability to invoke many packages to compute combinations and permutations. Combinat package in R programming language can be used to calculate permutations and combinations of the numbers.

How do you calculate permutations and combinations in R?

How to Calculate Combinations & Permutations in R You can use the following functions to calculate combinations and permutations in R: #calculate total combinations of size r from n total objects choose(n, r) #calculate total permutations of size r from n total objects choose(n, r) * factorial(r)


1 Answers

We can use a group by function with data.table

library(data.table)
setDT(sample)[, {i1 <-  combn(number, 2)
                   list(i1[1,], i1[2,]) }, by =  group]
#    group V1 V2
#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

Or a compact option would be

setDT(sample)[, transpose(combn(number, 2, FUN = list)), by = group]

Or using base R

 lst <- by(sample$number, sample$group, FUN = combn, m= 2)
 data.frame(group = rep(unique(as.character(sample$group)), 
                        sapply(lst, ncol)), t(do.call(cbind, lst)))
like image 94
akrun Avatar answered Nov 14 '22 20:11

akrun