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, decimals = 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.

decimals

Number of decimal places the percent should be rounded to

Value

The original data.frame found in data with two changes. The column called "pct" is multiplied by 100 and a new column is created called "pct_lab" that is the same as pct but with a "%" symbol at the end of it.

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%