inspect_cat: Summary and comparison of the levels in categorical columns

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/inspect_cat.R

Description

For a single dataframe, summarise the levels of each categorical column. If two dataframes are supplied, compare the levels of categorical features that appear in both dataframes. For grouped dataframes, summarise the levels of categorical features separately for each group.

Usage

1
inspect_cat(df1, df2 = NULL, include_int = FALSE)

Arguments

df1

A dataframe.

df2

An optional second data frame for comparing categorical levels. Defaults to NULL.

include_int

Logical flag - whether to treat integer columns as categories. Default is FALSE.

Details

For a single dataframe, the tibble returned contains the columns:

For a pair of dataframes, the tibble returned contains the columns:

For a grouped dataframe, the tibble returned is as for a single dataframe, but where the first k columns are the grouping columns. There will be as many rows in the result as there are unique combinations of the grouping variables.

Value

A tibble summarising or comparing the categorical features in one or a pair of dataframes.

Author(s)

Alastair Rushworth

See Also

inspect_imb, show_plot

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Load dplyr for starwars data & pipe
library(dplyr)

# Single dataframe summary
inspect_cat(starwars)

# Paired dataframe comparison
inspect_cat(starwars, starwars[1:20, ])

# Grouped dataframe summary
starwars %>% group_by(gender) %>% inspect_cat()

Example output

Attaching package:dplyrThe following objects are masked frompackage:stats:

    filter, lag

The following objects are masked frompackage:base:

    intersect, setdiff, setequal, union

# A tibble: 8 x 5
  col_name     cnt common    common_pcnt levels           
  <chr>      <int> <chr>           <dbl> <named list>     
1 eye_color     15 brown           24.1  <tibble [15 × 3]>
2 gender         3 masculine       75.9  <tibble [3 × 3]> 
3 hair_color    13 none            42.5  <tibble [13 × 3]>
4 homeworld     49 Naboo           12.6  <tibble [49 × 3]>
5 name          87 Ackbar           1.15 <tibble [87 × 3]>
6 sex            5 male            69.0  <tibble [5 × 3]> 
7 skin_color    31 fair            19.5  <tibble [31 × 3]>
8 species       38 Human           40.2  <tibble [38 × 3]>
# A tibble: 8 x 5
  col_name      jsd fisher_p lvls_1            lvls_2           
  <chr>       <dbl>    <dbl> <named list>      <named list>     
1 eye_color  0.0936 0.750    <tibble [15 × 3]> <tibble [8 × 3]> 
2 gender     0.0387 0.353    <tibble [3 × 3]>  <tibble [2 × 3]> 
3 hair_color 0.261  0.000843 <tibble [13 × 3]> <tibble [10 × 3]>
4 homeworld  0.394  0.359    <tibble [49 × 3]> <tibble [11 × 3]>
5 name       0.573  1.00     <tibble [87 × 3]> <tibble [20 × 3]>
6 sex        0.0526 0.287    <tibble [5 × 3]>  <tibble [4 × 3]> 
7 skin_color 0.288  0.990    <tibble [31 × 3]> <tibble [10 × 3]>
8 species    0.300  0.807    <tibble [38 × 3]> <tibble [6 × 3]> 
# A tibble: 21 x 6
# Groups:   gender [3]
   gender    col_name     cnt common     common_pcnt levels           
   <chr>     <chr>      <int> <chr>            <dbl> <named list>     
 1 feminine  eye_color      6 blue             35.3  <tibble [6 × 3]> 
 2 feminine  hair_color     6 brown            35.3  <tibble [6 × 3]> 
 3 feminine  homeworld     11 Naboo            17.6  <tibble [11 × 3]>
 4 feminine  name          17 Adi Gallia        5.88 <tibble [17 × 3]>
 5 feminine  sex            2 female           94.1  <tibble [2 × 3]> 
 6 feminine  skin_color     9 light            35.3  <tibble [9 × 3]> 
 7 feminine  species        8 Human            52.9  <tibble [8 × 3]> 
 8 masculine eye_color     13 brown            22.7  <tibble [13 × 3]>
 9 masculine hair_color    10 none             47.0  <tibble [10 × 3]>
10 masculine homeworld     44 Tatooine         12.1  <tibble [44 × 3]>
# … with 11 more rows

inspectdf documentation built on April 2, 2021, 5:07 p.m.