Add percentage for counts in the data.frame, both numeric and character with `

percent(x, digits = 1)

add_prop(.data, count_name = last(names(.data)), digits = 1)

Arguments

x

A number (numeric).

digits

How many digits to keep in the percentage. Default uses 1.

.data

A data frame.

count_name

Column name of counts (Character). Default uses the last column of data.frame.

References

https://stackoverflow.com/questions/7145826/how-to-format-a-number-as-percentage-in-r

Examples


 percent(0.9057)
#> [1] "90.6%"
 percent(0.9057,3)
#> [1] "90.570%"

 iris %>%
   count_dt(Species) %>%
   add_prop()
#>       Species     n      prop prop_label
#>        <fctr> <int>     <num>     <char>
#> 1:     setosa    50 0.3333333      33.3%
#> 2: versicolor    50 0.3333333      33.3%
#> 3:  virginica    50 0.3333333      33.3%

 iris %>%
   count_dt(Species) %>%
   add_prop(count_name = "n",digits = 2)
#>       Species     n      prop prop_label
#>        <fctr> <int>     <num>     <char>
#> 1:     setosa    50 0.3333333     33.33%
#> 2: versicolor    50 0.3333333     33.33%
#> 3:  virginica    50 0.3333333     33.33%