diff --git a/DESCRIPTION b/DESCRIPTION index 173d396..1f537a4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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) @@ -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' @@ -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' diff --git a/NAMESPACE b/NAMESPACE index 038a302..df23852 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/NEWS b/NEWS index 3f30c07..3370261 100644 --- a/NEWS +++ b/NEWS @@ -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. diff --git a/NEWS.md b/NEWS.md index f96a615..548bf72 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/f_abbreviation.R b/R/f_abbreviation.R new file mode 100644 index 0000000..4c82a5b --- /dev/null +++ b/R/f_abbreviation.R @@ -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) + + + + + + + diff --git a/R/f_year.R b/R/f_year.R new file mode 100644 index 0000000..8b9d32a --- /dev/null +++ b/R/f_year.R @@ -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) +} + + + + + + + + diff --git a/README.md b/README.md index badee64..3e34cfa 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ functions (see [Plotting](#plotting) for usage). - + @@ -86,22 +86,22 @@ alignment @@ -115,22 +115,22 @@ as_factor @@ -144,22 +144,22 @@ constant_months @@ -173,22 +173,22 @@ constant_months_abbreviation @@ -202,22 +202,22 @@ constant_weekdays @@ -231,22 +231,22 @@ f_12_hour @@ -255,26 +255,27 @@ fv_runs @@ -283,26 +284,27 @@ f_thous diff --git a/man/f_abbreviation.Rd b/man/f_abbreviation.Rd new file mode 100644 index 0000000..cf033dc --- /dev/null +++ b/man/f_abbreviation.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/f_abbreviation.R +\name{f_abbreviation} +\alias{f_abbreviation} +\alias{ff_abbreviation} +\title{Abbreviate Strings} +\usage{ +f_abbreviation(x, length = 5, ...) + +ff_abbreviation(...) +} +\arguments{ +\item{x}{A vector of text strings.} + +\item{length}{The minimum length of the abbreviations.} + +\item{\ldots}{Other arguments passed to \code{\link[base]{abbreviate}}.} +} +\value{ +Returns a string vector with strings abbreviated. +} +\description{ +A wrapper for \code{\link[base]{abbreviate}} for abbreviating strings. +} +\examples{ +f_abbreviation(state.name) +f_abbreviation('Cool Variable') +} +\seealso{ +\code{\link[base]{abbreviate}} +} diff --git a/man/f_year.Rd b/man/f_year.Rd new file mode 100644 index 0000000..f5ee002 --- /dev/null +++ b/man/f_year.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/f_year.R +\name{f_year} +\alias{f_year} +\alias{f_year.numeric} +\alias{f_year.Date} +\alias{f_year.POSIXlt} +\title{Format Years} +\usage{ +f_year(x, digits = 2, ...) + +\method{f_year}{numeric}(x, digits = 2, ...) + +\method{f_year}{Date}(x, digits = 2, ...) + +\method{f_year}{POSIXlt}(x, digits = 2, ...) +} +\arguments{ +\item{x}{A vector of 4 digits integers, dates, or POSIXlt.} + +\item{digits}{Either 2 or 4 for the number of digits to make the year.} + +\item{\ldots}{ignored.} +} +\value{ +Returns a vector of two or four digit years. +} +\description{ +Format 4 digit integer, date, or POSIXlt formats to 2 or 4 digit years. +} +\examples{ +f_year(as.Date(paste0(1998:2016, '-12-12'))) +f_year(c(NA, 1998:2016, 21345)) +}
-f_bills +f_affix -f_month +f_mills -f_prop2percent +f_prefix -f_weekday +f_title
-f_comma +f_bills -f_num +f_month -f_pval +f_prop2percent -f_wrap +f_weekday
-f_date +f_comma -f_num_percent +f_num -f_replace +f_pval -fv_num_percent +f_wrap
-f_denom +f_date -f_ordinal +f_num_percent -f_sign +f_replace -fv_percent +f_year
-f_dollar +f_denom -f_pad_zero +f_ordinal -f_state +f_sign -fv_percent_diff +fv_num_percent
-f_logical +f_dollar -f_parenthesis +f_pad_zero -f_suffix +f_state -fv_runs +fv_percent
-f_affirm +f_abbreviation -f_mean_sd +f_logical -f_percent +f_parenthesis -f_thous +f_suffix +fv_percent_diff
-f_affix +f_affirm -f_mills +f_mean_sd -f_prefix +f_percent -f_title +f_thous +fv_runs