I am looking for an easy way to count the number of
unique combinations in a big binary matrix
.
If I have the following matrix
:
m = matrix(c(1,0,1,1,1,1,1,0,1,1,0,1), ncol=3, byrow=TRUE)
I would like to get the following output:
[,1] [,2] [,3] [,4]
[1,] 1 0 1 3
[2,] 1 1 1 1
So the function should count the number of unique combinations of columns - there are three rows that contains 1,0,1, and one row that contains 1,1,1.
The object type is not fixed (i.e. it can be a data.frame
, data.table
, whatever).
I would like to avoid having to specify the columns, to count over, by hand,
if possible (i.e. i can supply a character vector with the corresponding column names, or use the whole matrix).
I know there has to be a solution, but my google fu seems to weak.