Skip to content

Commit

Permalink
* fv_percent_lead added to work similar to fv_percent_diff but us…
Browse files Browse the repository at this point in the history
…ing the

  formula T_n/T_n-1.

* `fv_percent_lead_fixed_relative` & `fv_percent_diff_fixed_relative` added in
  order to compute `fv_percent_lead` & `fv_percent_diff` but keeping the
  comparison year as a constant rather than using the prior year in the vector
  of elements.
  • Loading branch information
Tyler Rinker committed Oct 22, 2018
1 parent ff584d7 commit 0071c12
Show file tree
Hide file tree
Showing 24 changed files with 254 additions and 38 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Imports: glue
License: GPL-2
LazyData: TRUE
Roxygen: list(wrap = FALSE)
RoxygenNote: 6.0.1
RoxygenNote: 6.1.0.9000
Collate:
'alignment.R'
'as_factor.R'
Expand Down Expand Up @@ -50,6 +50,7 @@ Collate:
'fv_num_percent.R'
'fv_percent.R'
'fv_percent_diff.R'
'fv_percent_lead.R'
'fv_runs.R'
'glue-reexports.R'
'highlight_cells.R'
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,15 @@ export(ff_year)
export(ffv_num_percent)
export(ffv_percent)
export(ffv_percent_diff)
export(ffv_percent_diff_fixed_relative)
export(ffv_percent_lead)
export(ffv_percent_lead_fixed_relative)
export(fv_num_percent)
export(fv_percent)
export(fv_percent_diff)
export(fv_percent_diff_fixed_relative)
export(fv_percent_lead)
export(fv_percent_lead_fixed_relative)
export(fv_runs)
export(glue)
export(highlight_cells)
Expand Down
8 changes: 8 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ NEW FEATURES
a more human readable for. There are several different forms of `f_bin` so
use `?f_bin` to see them all.

* `fv_percent_lead` added to work similar to `fv_percent_diff` but using the
formula T_n/T_n-1.

* `fv_percent_lead_fixed_relative` & `fv_percent_diff_fixed_relative` added in
order to compute `fv_percent_lead` & `fv_percent_diff` but keeping the
comparison year as a constant rather than using the prior year in the vector
of elements.

MINOR FEATURES

* `f_denom` family of functions picks up a `less.than.replace` argument to
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ numform 0.4.1 -
a more human readable for. There are several different forms of `f_bin` so
use `?f_bin` to see them all.

* `fv_percent_lead` added to work similar to `fv_percent_diff` but using the
formula T_n/T_n-1.

* `fv_percent_lead_fixed_relative` & `fv_percent_diff_fixed_relative` added in
order to compute `fv_percent_lead` & `fv_percent_diff` but keeping the
comparison year as a constant rather than using the prior year in the vector
of elements.

**MINOR FEATURES**

* `f_denom` family of functions picks up a `less.than.replace` argument to
Expand Down
27 changes: 26 additions & 1 deletion R/fv_percent_diff.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Percent Difference
#'
#' Convert a vector of values to percent differences.
#' \code{fv_percent_diff} - Convert a vector of values to percent differences
#' (i.e., (T2 - T1)/T1).
#'
#' @param x A numeric vector.
#' @param digits The number of digits to use. Defaults to 1. Can be set
Expand Down Expand Up @@ -42,6 +43,30 @@ fv_percent_diff <- function(x, digits = getOption("numformdigits"), ...){
f_prop2percent(c(0, (x[-1] - x[-length(x)])/x[-length(x)]), digits = digits, ...)
}


#' \code{fv_percent_diff_fixed_relative} - Similar to \code{fv_percent_diff} but
#' the comparison year is a constant rather than the prior year. This is
#' particularly useful for seeing changes relative to time 1.
#' @param fixed.relative The position of the element to be used for comparison.
#' Default is the first element.
#' @export
#' @include utils.R
#' @rdname fv_percent_diff
fv_percent_diff_fixed_relative <- function(x, fixed.relative = 1, digits = getOption("numformdigits"), ...){
f_prop2percent(c(0, x[-1] - x[fixed.relative])/x[fixed.relative], digits = digits, ...)
}




#' @export
#' @include utils.R
#' @rdname fv_percent_diff
ffv_percent_diff_fixed_relative <- functionize(fv_percent_diff_fixed_relative)




#' @export
#' @include utils.R
#' @rdname fv_percent_diff
Expand Down
76 changes: 76 additions & 0 deletions R/fv_percent_lead.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#' Percent Difference
#'
#' \code{fv_percent_lead} - Convert a vector of values to percent relative to
#' prior value in the vector (i.e., T2/T1).
#'
#' @param x A numeric vector.
#' @param digits The number of digits to use. Defaults to 1. Can be set
#' globally via: \code{options(numformdigits = n)} where n is the number of
#' digits beyond the decimal point to include.
#' @param \ldots Other arguments passed to \code{\link[numform]{f_prop2percent}}.
#' @return Returns a string of publication ready relative percent differences.
#' @rdname fv_percent_lead
#' @export
#' @examples
#' set.seed(10)
#' x <- sample(1:10)
#'
#' data.frame(
#' original = x,
#' perc_change = fv_percent_lead(x)
#' )
#'
#' \dontrun{
#' library(dplyr)
#'
#' CO2 %>%
#' group_by(Plant) %>%
#' mutate(
#' `Percent` = fv_percent(conc),
#' `Percent Diff` = fv_percent_diff(conc),
#' `Percent Relative` = fv_percent_lead(conc)
#' ) %>%
#' print(n=Inf)
#'
#' CO2 %>%
#' group_by(Type, Treatment) %>%
#' mutate(
#' `Percent` = fv_percent(conc),
#' `Percent Diff` = fv_percent_diff(conc),
#' `Percent Relative` = fv_percent_lead(conc)
#' ) %>%
#' print(n=Inf)
#' }
fv_percent_lead <- function(x, digits = getOption("numformdigits"), ...){
f_prop2percent(c(0, x[-1]/x[-length(x)]), digits = digits, ...)
}


#' \code{fv_percent_lead_fixed_relative} - Similar to \code{fv_percent_lead} but
#' the comparison year is a constant rather than the prior year. This is
#' particularly useful for seeing changes relative to time 1.
#' @param fixed.relative The position of the element to be used for comparison.
#' Default is the first element.
#' @export
#' @include utils.R
#' @rdname fv_percent_lead
fv_percent_lead_fixed_relative <- function(x, fixed.relative = 1, digits = getOption("numformdigits"), ...){
f_prop2percent(c(0, x[-1])/x[fixed.relative], digits = digits, ...)
}



#' @export
#' @include utils.R
#' @rdname fv_percent_lead
ffv_percent_lead <- functionize(fv_percent_lead)


#' @export
#' @include utils.R
#' @rdname fv_percent_lead
ffv_percent_lead_fixed_relative <- functionize(fv_percent_lead_fixed_relative)




13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ arguments but returns a function instead. This is useful for passing in
to **ggplot2** `scale_x/y_type` functions (see [Plotting](#plotting) for
usage).

<!-- html table generated in R 3.6.0 by xtable 1.8-2 package -->
<!-- Sat Jun 23 07:55:38 2018 -->
<!-- html table generated in R 3.5.1 by xtable 1.8-3 package -->
<!-- Mon Oct 22 11:36:18 2018 -->
<table>
<tr>
<td>
Expand Down Expand Up @@ -209,7 +209,7 @@ f_mills
f_replace
</td>
<td>
fv_runs
fv_percent_diff_fixed_relative
</td>
</tr>
<tr>
Expand All @@ -226,7 +226,7 @@ f_month
f_response
</td>
<td>
glue
fv_percent_lead
</td>
</tr>
<tr>
Expand All @@ -243,7 +243,7 @@ f_num
f_sign
</td>
<td>
highlight_cells
fv_percent_lead_fixed_relative
</td>
</tr>
<tr>
Expand All @@ -260,6 +260,7 @@ f_num_percent
f_state
</td>
<td>
fv_runs
</td>
</tr>
<tr>
Expand All @@ -276,6 +277,7 @@ f_ordinal
f_suffix
</td>
<td>
glue
</td>
</tr>
<tr>
Expand All @@ -292,6 +294,7 @@ f_pad_zero
f_thous
</td>
<td>
highlight_cells
</td>
</tr>
</table>
Expand Down
9 changes: 6 additions & 3 deletions man/f_12_hour.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 14 additions & 12 deletions man/f_bin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/f_degree.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions man/f_denom.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/f_percent.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/f_quarter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/fv_percent.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0071c12

Please sign in to comment.