Search code examples
rmatrixunique

How to find unique elements in matrix (per individual cells) and the number of their occurences?


I would like to get all unique elements in a dataframe and know how many times they occur over the matrix, think of something like this:

example

I'd want a matrix indicating "11" occurs 1 time etc.

sort(unique(c(matrix))) is how far I got.

As this is more of a gimmick to a combinatorial problem, is a matrix the best solution to this anyway?

Thank you for any help for a beginner !


Solution

  • We can directly use table to get the frequency of all the unique elements in the matrix

    table(matrix)