Open Nic-Chr opened 3 months ago
This was agreed to take forward as a PR
There are a few things I'm not sure about regarding the implementation from a user-perspective.
percent()
and as_percent()
.
percent()
simply creates a percent vector from percentage inputs, e.g. 100 becomes 100%.
as_percent()
converts proportions to percentages.
It's not clear to me which is more intuitive from a user-friendly perspective and if we should just use 1 or both or something a bit different?as.character.percent()
apply rounding by default? The reason I opted for this is because it plays nicely with ggplot2 which relies on calling as.character
in plots, which makes things easier to read. On the other hand a user might expect to see all the underlying digits when using as.character.percent()
. format.percent()
by default applies decimal digit rounding instead of the usual significant digit rounding that format()
uses. This is because imo decimal rounding is much nicer for percentages generally and hence is more useful for users. A solution would be to just make this distinction clear in the documentation.
Motivation
Working with percentages in R can be annoying to say the least and in day-to-day analyses I tend to find myself in this general workflow:
Having a percent class object could reduce this workflow by combining the two vectors into one, reducing the work needed to manage independent vectors.
Describe the solution you'd like It would be nice to see a percent class that represents proportions without losing precision and simply prints them as percentages. This would help analysts across PHS spend less time thinking about how to format percentages.
Describe alternatives you've considered I have made a small package that does this, see: github.com/NicChr/percent I'm aware of
scales::percent()
but this returns a character vector whereasas_percent()
does no transformations at all, returning an object of class "percent", preserving the proportions vector and printing as a "percent" vector in tibbles.@Tina815 @Moohan Let me know if you think this would be a good fit for phsmethods and if so I'd be happy to assist in future implementations.
If this was deemed to be a good fit, I would be happy for the code to be copied over, reducing the need for another package dependency.
Below I've included some basic examples.
Basic usage
Created on 2024-03-07 with reprex v2.0.2