
Functions as_
functions_as.Rmd
as_
functions coerce values to another type.
as_num
The goal of as_num
is to be a more generalized version
of as.numeric
, where the input is a string with numbers and
marks, such as “10.000,02”.
as_num("123.456,78")
#> [1] 123456.8
as_perc
The goal of as_perc
is to convert a number to a
percentage. By default, the function simply multiplies values by
100.
mtcars %>%
count(vs,am) %>%
mutate(prop = n/sum(n)) %>%
mutate(perc = as_perc(prop))
#> vs am n prop perc
#> 1 0 0 12 0.37500 37.500
#> 2 0 1 6 0.18750 18.750
#> 3 1 0 7 0.21875 21.875
#> 4 1 1 7 0.21875 21.875
If you set the argument sum
to TRUE
the
function will divide the values by their total and multiply by 100.