Skip to contents

This function calculates the number of unique values for each column in a given data frame and returns a data frame with two columns: Variable and NUnique. The Variable column lists the names of the original columns, and the NUnique column lists the corresponding number of unique values in each column. The result is sorted by the number of unique values in ascending order.

Usage

NUnique(Data)

Source

The source code of the function was copied from this stackoverflow question.

Arguments

Data

A data frame for which the number of unique values per column will be calculated.

Value

A data frame with two columns: Variable and NUnique. The Variable column lists the names of the original columns, and the NUnique column lists the number of unique values in each column. The result is sorted by NUnique in ascending order.

Examples

NUnique(mtcars)
#> # A tibble: 11 × 2
#>    Variable NUnique
#>    <chr>      <int>
#>  1 vs             2
#>  2 am             2
#>  3 cyl            3
#>  4 gear           3
#>  5 carb           6
#>  6 hp            22
#>  7 drat          22
#>  8 mpg           25
#>  9 disp          27
#> 10 wt            29
#> 11 qsec          30

NUnique(iris)
#> # A tibble: 5 × 2
#>   Variable     NUnique
#>   <chr>          <int>
#> 1 Species            3
#> 2 Petal.Width       22
#> 3 Sepal.Width       23
#> 4 Sepal.Length      35
#> 5 Petal.Length      43