Search code examples
rdataframecountunique

Count unique values for every column


I would like to return the count of the unique (distinct) values for every column in a data frame. For example, if I have the table:

 Testdata <- data.frame(var_1 = c("a","a","a"), var_2 = c("b","b","b"), var_3 = c("c","d","e"))

 var_1 | var_2 | var_3
 a     | b     | c 
 a     | b     | d
 a     | b     | e

I would like the output to be:

 Variable | Unique_Values
 var_1    | 1
 var_2    | 1
 var_3    | 3

I have tried playing around with loops using the unique function, e.g.

 for(i in names(Testdata)){
    # Code using unique function
 }

However I suspect there is a simpler way.


Solution

  • You could use apply:

    apply(Testdata, 2, function(x) length(unique(x)))
    # var_1 var_2 var_3 
    #     1     1     3