Skip to content

Commit

Permalink
* f_year added as a ggplot2 scale to convert to 2 digit year form c…
Browse files Browse the repository at this point in the history
…onvenience

  function.

* `f_abbreviation` added for converting string names to their abbreviated form.
  • Loading branch information
trinker committed Sep 8, 2017
1 parent 8284054 commit 33459d9
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 38 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Description: Format numbers and plots for publication; includes the removal of l
deploy.
Depends: R (>= 3.2.0)
Suggests: testthat
Date: 2017-09-06
Date: 2017-09-07
License: GPL-2
LazyData: TRUE
Roxygen: list(wrap = FALSE)
Expand All @@ -23,6 +23,7 @@ Collate:
'constants.R'
'f_12_hour.R'
'utils.R'
'f_abbreviation.R'
'f_affirm.R'
'f_affix.R'
'f_comma.R'
Expand All @@ -43,6 +44,7 @@ Collate:
'f_title.R'
'f_weekday.R'
'f_wrap.R'
'f_year.R'
'fv_num_percent.R'
'fv_percent.R'
'fv_percent_diff.R'
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ S3method(f_weekday,Date)
S3method(f_weekday,POSIXlt)
S3method(f_weekday,default)
S3method(f_weekday,numeric)
S3method(f_year,Date)
S3method(f_year,POSIXlt)
S3method(f_year,numeric)
export(alignment)
export(as_factor)
export(constant_months)
export(constant_months_abbreviation)
export(constant_weekdays)
export(f_12_hour)
export(f_abbreviation)
export(f_affirm)
export(f_affix)
export(f_bills)
Expand Down Expand Up @@ -45,6 +49,8 @@ export(f_thous)
export(f_title)
export(f_weekday)
export(f_wrap)
export(f_year)
export(ff_abbreviation)
export(ff_affirm)
export(ff_affix)
export(ff_bills)
Expand Down
10 changes: 8 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ NEW FEATURES
* `f_date` and `f_12_hour` added to convert dates and times into common in-text
form.

* `f_replace` added as a ggplot2 scale `gsub` convenience function.
* `f_replace` added as a ggplot2 scale `gsub` convenience function. Defaults
to replace '_' with ' '.

* `f_title` added as a ggplot2 scale `tools::toTitleCase` convenience function.

* `f_title` added as a ggplot2 scale `strwrap` + `paste(collapse =TRUE)`
* `f_wrap` added as a ggplot2 scale `strwrap` + `paste(collapse =TRUE)`
convenience function.

* `f_state` added for converting state names to their abbreviated form for plots.

* `f_year` added as a ggplot2 scale to convert to 2 digit year form convenience
function.

* `f_abbreviation` added for converting string names to their abbreviated form.

* constants added for weekdays, months, and month abbreviations in order to make
factor conversion of levels easier. See `?constant_months` for more.

Expand Down
10 changes: 8 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ numform 0.0.6 -
* `f_date` and `f_12_hour` added to convert dates and times into common in-text
form.

* `f_replace` added as a ggplot2 scale `gsub` convenience function.
* `f_replace` added as a ggplot2 scale `gsub` convenience function. Defaults
to replace '_' with ' '.

* `f_title` added as a ggplot2 scale `tools::toTitleCase` convenience function.

* `f_title` added as a ggplot2 scale `strwrap` + `paste(collapse =TRUE)`
* `f_wrap` added as a ggplot2 scale `strwrap` + `paste(collapse =TRUE)`
convenience function.

* `f_state` added for converting state names to their abbreviated form for plots.

* `f_year` added as a ggplot2 scale to convert to 2 digit year form convenience
function.

* `f_abbreviation` added for converting string names to their abbreviated form.

* constants added for weekdays, months, and month abbreviations in order to make
factor conversion of levels easier. See `?constant_months` for more.

Expand Down
32 changes: 32 additions & 0 deletions R/f_abbreviation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Abbreviate Strings
#'
#' A wrapper for \code{\link[base]{abbreviate}} for abbreviating strings.
#'
#' @param x A vector of text strings.
#' @param length The minimum length of the abbreviations.
#' @param \ldots Other arguments passed to \code{\link[base]{abbreviate}}.
#' @return Returns a string vector with strings abbreviated.
#' @rdname f_abbreviation
#' @export
#' @seealso \code{\link[base]{abbreviate}}
#' @examples
#' f_abbreviation(state.name)
#' f_abbreviation('Cool Variable')
f_abbreviation <- function (x, length = 5, ...) {

abbreviate(x, minlength = length, named = FALSE, ...)

}


#' @export
#' @include utils.R
#' @rdname f_abbreviation
ff_abbreviation <- functionize(f_abbreviation)







59 changes: 59 additions & 0 deletions R/f_year.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' Format Years
#'
#' Format 4 digit integer, date, or POSIXlt formats to 2 or 4 digit years.
#'
#' @param x A vector of 4 digits integers, dates, or POSIXlt.
#' @param digits Either 2 or 4 for the number of digits to make the year.
#' @param \ldots ignored.
#' @return Returns a vector of two or four digit years.
#' @export
#' @rdname f_year
#' @examples
#' f_year(as.Date(paste0(1998:2016, '-12-12')))
#' f_year(c(NA, 1998:2016, 21345))
f_year <- function(x, digits = 2, ...) {
UseMethod('f_year')
}




#' @export
#' @rdname f_year
#' @method f_year numeric
f_year.numeric <- function(x, digits = 2, ...) {

x[!grepl('^\\d{4}$', as.character(x))] <- NA

switch(ifelse(digits == 2, 'two', ifelse(digits == 4, 'four', 'three')),
two = gsub('(^\\d{2})(\\d{2}$)', '\\2', as.character(as.integer(x))),
four = gsub('(^\\d{2})(\\d{2}$)', '\\1\\2', as.character(as.integer(x))),
stop('`digits` must be either 2 or 4')
)
}




#' @export
#' @rdname f_year
#' @method f_year Date
f_year.Date <- function(x, digits = 2, ...) {
f_year(as.integer(format(x, '%Y')), digits = digits)
}


#' @export
#' @rdname f_year
#' @method f_year POSIXlt
f_year.POSIXlt <- function(x, digits = 2, ...) {
f_year.Date(x, digits = digits)
}








Loading

0 comments on commit 33459d9

Please sign in to comment.