Reputation: 309
I have a very big data frame but for the sake of this question let's consider a subset of it which follows;
i j k l m n o p q
1 1 1 1 1 1 3 4 4
1 1 1 1 1 1 4 3 4
1 1 1 1 1 1 4 4 3
1 1 1 1 1 2 2 4 4
1 1 1 1 1 2 3 3 4
1 1 1 1 1 2 3 4 3
In the above data frame values that are in the rows can be arranged in a particular number of ways; for example, let's consider the first row which is (1,1,1,1 1,1,3,4,4). Any assistance leading in computing this number of ways in R will be highly appreciated.
Upvotes: 1
Views: 361
Reputation: 44340
You can calculate the number of unique permutations of each row with:
apply(dat, 1, function(x) factorial(length(x)) / prod(factorial(table(x))))
# [1] 252 252 252 756 1512 1512
If all elements of a row of length n were unique there would be n! (n factorial) permutations. However, if there are k1, k2, ..., kj total copies of each of the j unique elements, then we can reverse the overcounting of duplicates by dividing by k1! * k2! * ... * kj!. There are more details about multinomial coefficients here.
If two rows have identical elements (possibly in a different ordering) then all their permutations will also be identical. We can take this into account by checking for duplicate ordered rows:
apply(dat, 1, function(x) factorial(length(x)) / prod(factorial(table(x)))) *
!duplicated(t(apply(dat, 1, sort)))
# [1] 252 0 0 756 1512 0
Upvotes: 5