-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*
f_year
added as a ggplot2 scale to convert to 2 digit year form c…
…onvenience function. * `f_abbreviation` added for converting string names to their abbreviated form.
- Loading branch information
Showing
9 changed files
with
216 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.