Use prop.table() to add a proportion column to a dplyr::count() tibble.

add_prop(.data, n, sum = FALSE)

Arguments

.data

A data frame with a count column.

n

The column name with a count, usually n from dplyr::count().

sum

Should cumsum() be called on the new p column.

Value

A data frame with the new column p.

Details

mean(x %in% y)

Examples

add_prop(dplyr::count(ggplot2::diamonds, cut))
#> # A tibble: 5 × 3
#>   cut           n      p
#>   <ord>     <int>  <dbl>
#> 1 Fair       1610 0.0298
#> 2 Good       4906 0.0910
#> 3 Very Good 12082 0.224 
#> 4 Premium   13791 0.256 
#> 5 Ideal     21551 0.400