Search code examples
rdataframeunique

How do I calculate total number of unique values in a dataframe?


Here's a sample dataframe:

>frame

  X1 X2 X3 X4
1  F  F  F  F
2  C  C  C  C
3  D  D  D  D
4 A# A# A# A#

When I run unique(c(frame)) I get a vector of the four unique values, "F", "C", "D", "A#". However, if I run length(unique(c(frame))), I get a length of 1. So I'm looking for a way to get the total number of unique values in a dataframe, and also in a subset of the dataframe.


Solution

  • Per @DatamineR:

    length(unique(unlist(df)))