Skip to content

Commit

Permalink
* f_date and f_12_hour added to convert dates and times into comm…
Browse files Browse the repository at this point in the history
…on in-text

  form.

close #8
  • Loading branch information
trinker committed Aug 12, 2017
1 parent d408dee commit 53196db
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 22 deletions.
12 changes: 8 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ Title: Tools to Format Numbers for Publication
Version: 0.1.1
Authors@R: c(person("Tyler", "Rinker", email = "tyler.rinker@gmail.com", role = c("aut", "cre")))
Maintainer: Tyler Rinker <tyler.rinker@gmail.com>
Description: Format numbers for publication; includes the removal of leading zeros, standardization of number of digits, addition of affixes, and a p-value formatter. These tools
combine the functionality of several 'base' functions such as paste(), format(), and sprintf() into specific use case functions that are named in a way that is
consistent with usage, making their names easy to remember and easy to deploy.
Description: Format numbers for publication; includes the removal of leading zeros, standardization of number
of digits, addition of affixes, and a p-value formatter. These tools combine the functionality of
several 'base' functions such as paste(), format(), and sprintf() into specific use case
functions that are named in a way that is consistent with usage, making their names easy to
remember and easy to deploy.
Depends: R (>= 3.2.0)
Suggests: testthat
Date: 2017-08-10
Date: 2017-08-11
License: GPL-2
LazyData: TRUE
Roxygen: list(wrap = FALSE)
Expand All @@ -17,9 +19,11 @@ Collate:
'alignment.R'
'as_factor.R'
'utils.R'
'f_12_hour.R'
'f_affirm.R'
'f_affix.R'
'f_comma.R'
'f_date.R'
'f_denom.R'
'f_dollar.R'
'f_logical.R'
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ S3method(f_weekday,default)
S3method(f_weekday,numeric)
export(alignment)
export(as_factor)
export(f_12_hour)
export(f_affirm)
export(f_affix)
export(f_bills)
export(f_comma)
export(f_date)
export(f_denom)
export(f_dollar)
export(f_logical)
Expand All @@ -33,10 +35,12 @@ export(f_sign)
export(f_suffix)
export(f_thous)
export(f_weekday)
export(ff_12_hour)
export(ff_affirm)
export(ff_affix)
export(ff_bills)
export(ff_comma)
export(ff_date)
export(ff_denom)
export(ff_dollar)
export(ff_logical)
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ NEW FEATURES
add-on R packages which may be confused by **numform**'s conversion of numeric
to character class, though right table alignment should be maintained.

* `f_date` and `f_12_hour` added to convert dates and times into common in-text
form.

MINOR FEATURES

* `f_sign` picks up `negative` and `positive` assignments allowing for more
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ numform 0.0.6 -
add-on R packages which may be confused by **numform**'s conversion of numeric
to character class, though right table alignment should be maintained.

* `f_date` and `f_12_hour` added to convert dates and times into common in-text
form.

**MINOR FEATURES**

* `f_sign` picks up `negative` and `positive` assignments allowing for more
Expand Down
38 changes: 38 additions & 0 deletions R/f_12_hour.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' Format 12 Hour Times
#'
#' Format times to the typical 12 hour '%I:%M %p' in-text format.
#'
#' @param x A vector of coercable times.
#' @param format A character string specifying the time output format.
#' @param pad.char A character to use for leading padding if lengths of output
#' are unequal.
#' @param \ldots Other arguments passed to \code{\link[base]{as.POSIXct}}.
#' @return Returns a string of publication ready 12 hour time stamps.
#' @export
#' @rdname f_12_hour
#' @examples
#' f_12_hour(Sys.time())
#' f_12_hour(Sys.time(), pad.char ='0')
#' f_12_hour(Sys.time(), pad.char =' ')
#' f_12_hour(Sys.time(), '%I:%M:%S %p')
#' set.seed(10)
#' times <- as.POSIXct(sample(seq_len(1e4), 12), origin = '1970-01-01')
#' paste(f_12_hour(range(times)), collapse = ' to ')
f_12_hour <- function(x, format = '%I:%M %p', pad.char = '', ...){

out <- format(as.POSIXct(x, ...), format = format)
gsub('^0', pad.char, out)

}


#' @export
#' @include utils.R
#' @rdname f_12_hour
ff_12_hour <- functionize(f_12_hour)






28 changes: 28 additions & 0 deletions R/f_date.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' Format Dates
#'
#' Format dates to the typical '%B %d, %Y in-text format.
#'
#' @param x A vector of coercable dates.
#' @param format A character string specifying the date output format.
#' @param \ldots Other arguments passed to \code{\link[base]{as.Date}}.
#' @return Returns a string of publication ready dates.
#' @export
#' @rdname f_date
#' @examples
#' f_date(Sys.Date())
#' f_date(Sys.time())
#' f_date(Sys.time(), '%b-%y')
#' set.seed(10)
#' dates <- as.Date(sample(1:10000, 12), origin = '1970-01-01')
#' paste(f_date(range(dates)), collapse = ' to ')
f_date <- function(x, format = '%B %d, %Y', ...){

format(as.Date(x, ...), format = format)

}

#' @export
#' @include utils.R
#' @rdname f_date
ff_date <- functionize(f_date)

62 changes: 44 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ functions (see [Plotting](#plotting) for usage).

<!-- html table generated in R 3.4.1 by xtable 1.8-2 package -->

<!-- Thu Aug 10 23:22:33 2017 -->
<!-- Fri Aug 11 21:55:45 2017 -->

<table>

Expand All @@ -86,7 +86,7 @@ alignment

<td>

f_denom
f_date
</td>

<td>
Expand All @@ -96,12 +96,12 @@ f_num

<td>

f_prefix
f_prop2percent
</td>

<td>

f_weekday
fv_percent
</td>

</tr>
Expand All @@ -115,7 +115,7 @@ as_factor

<td>

f_dollar
f_denom
</td>

<td>
Expand All @@ -125,12 +125,41 @@ f_num_percent

<td>

f_prop2percent
f_pval
</td>

<td>

fv_num_percent
fv_percent_diff
</td>

</tr>

<tr>

<td>

f_12_hour
</td>

<td>

f_dollar
</td>

<td>

f_ordinal
</td>

<td>

f_sign
</td>

<td>

fv_runs
</td>

</tr>
Expand All @@ -149,17 +178,16 @@ f_logical

<td>

f_ordinal
f_pad_zero
</td>

<td>

f_pval
f_suffix
</td>

<td>

fv_percent
</td>

</tr>
Expand All @@ -178,17 +206,16 @@ f_mean_sd

<td>

f_pad_zero
f_parenthesis
</td>

<td>

f_sign
f_thous
</td>

<td>

fv_percent_diff
</td>

</tr>
Expand All @@ -207,17 +234,16 @@ f_mills

<td>

f_parenthesis
f_percent
</td>

<td>

f_suffix
f_weekday
</td>

<td>

fv_runs
</td>

</tr>
Expand All @@ -236,12 +262,12 @@ f_month

<td>

f_percent
f_prefix
</td>

<td>

f_thous
fv_num_percent
</td>

<td>
Expand Down
36 changes: 36 additions & 0 deletions man/f_12_hour.Rd

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

32 changes: 32 additions & 0 deletions man/f_date.Rd

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

0 comments on commit 53196db

Please sign in to comment.