count_pct is the extenstion of dplyr::count with percent count_pct_group is the extenstion of dplyr::count with percent by group

count_pct(.data, ..., sort = FALSE, accuracy = 3)

count_pct_group(.data, ..., accuracy = 3)

Value

returns a dataframe with count and percentage columns

Examples

library(dacol, warn.conflicts = FALSE) library(dplyr, warn.conflicts = FALSE) library(scales, warn.conflicts = FALSE) df = data.frame(x = c(1, 2, 3, 4, 5), y = c("b", "a", "k", "b", "k"), z = c(5, 8, 8, 6, 5)) df %>% count_pct(y)
#> # A tibble: 3 x 3 #> y n pt #> <chr> <int> <chr> #> 1 a 1 21% #> 2 b 2 39% #> 3 k 2 39%
df %>% count_pct(z)
#> # A tibble: 3 x 3 #> z n pt #> <dbl> <int> <chr> #> 1 5 2 39% #> 2 6 1 21% #> 3 8 2 39%
mtcars %>% count_pct(cyl)
#> # A tibble: 3 x 3 #> cyl n pt #> <dbl> <int> <chr> #> 1 4 11 33% #> 2 6 7 21% #> 3 8 14 45%
mtcars %>% count_pct(cyl, sort = TRUE)
#> # A tibble: 3 x 3 #> cyl n pt #> <dbl> <int> <chr> #> 1 8 14 45% #> 2 4 11 33% #> 3 6 7 21%
mtcars %>% count_pct(cyl, am, sort = TRUE)
#> # A tibble: 6 x 4 #> cyl am n pt #> <dbl> <dbl> <int> <chr> #> 1 8 0 12 36% #> 2 4 1 8 24% #> 3 6 0 4 12% #> 4 4 0 3 9% #> 5 6 1 3 9% #> 6 8 1 2 6%
mtcars %>% count_pct_group(cyl)
#> # A tibble: 3 x 3 #> cyl n pt #> <dbl> <int> <chr> #> 1 4 11 33% #> 2 6 7 21% #> 3 8 14 45%
mtcars %>% count_pct_group(cyl, am)
#> # A tibble: 6 x 4 #> # Groups: cyl [3] #> cyl am n pt #> <dbl> <dbl> <int> <chr> #> 1 4 0 3 27% #> 2 4 1 8 72% #> 3 6 0 4 57% #> 4 6 1 3 42% #> 5 8 0 12 87% #> 6 8 1 2 15%
mtcars %>% count_pct_group(cyl, am) %>% arrange(cyl, desc(n))
#> # A tibble: 6 x 4 #> # Groups: cyl [3] #> cyl am n pt #> <dbl> <dbl> <int> <chr> #> 1 4 1 8 72% #> 2 4 0 3 27% #> 3 6 0 4 57% #> 4 6 1 3 42% #> 5 8 0 12 87% #> 6 8 1 2 15%