Skip to contents

This function is a wrapper around a very common data transformation that is done every time we make a frequency plot

Usage

pct_conv(data, x = pct, digits = 1)

Arguments

data

A data frame or vector. Can be left blank if used during piping

x

a variable we want to convert to a percentage. The value is pct by default and should always be pct.

digits

Number of decimal places the percent should be rounded to

Examples


library(tibble)
library(dplyr)
library(labelled)
library(haven)

# create the fake data
df <- tibble::tribble(
  ~x, ~y, ~z,
  3, 2, 3,
  4, 4, 2,
  2, 6, 1,
  1, 1, 4,
  5, 4, 3,
  6, 5, 6
)

df %>%
  count(x) %>%
  mutate(pct = prop.table(n)) %>%
  pct_conv()
#> # A tibble: 6 × 4
#>       x     n   pct pct_lab
#>   <dbl> <int> <dbl> <chr>  
#> 1     1     1  16.7 16.7%  
#> 2     2     1  16.7 16.7%  
#> 3     3     1  16.7 16.7%  
#> 4     4     1  16.7 16.7%  
#> 5     5     1  16.7 16.7%  
#> 6     6     1  16.7 16.7%