Humanize is an almost direct port of the python humanize package.

The goal of humanize is to provide some utlities in order to turn values (so far times, file sizes, and numbers) into human readable forms.

Installation

You can install the latest CRAN version with:

install.packages("humanize")

You can install humanize from github with:

# install.packages("devtools")
devtools::install_github("newtux/humanize")

Examples

Times

Convert times:

library(humanize)

natural_time(Sys.time())
#> [1] "now"
natural_time(Sys.time() - 1)
#> [1] "a second ago"
natural_time(Sys.time() - 100)
#> [1] "a minute ago"
natural_time(Sys.time() - 1000*10)
#> [1] "2 hours ago"

Works across days:

natural_time(Sys.time() - lubridate::ddays(1))
#> [1] "a day ago"
natural_time(Sys.time() - lubridate::ddays(70))
#> [1] "2 months ago"

And forward in time:

natural_time(Sys.time() + lubridate::ddays(1))
#> [1] "23 hours from now"

File Sizes

Convert file sizes:

natural_size(300)
#> 300 Bytes
natural_size(3000)
#> 3.0 kB
natural_size(3000000)
#> 3.0 MB
natural_size(3000000000)
#> 3.0 GB
natural_size(3000000000000)
#> 3.0 TB
natural_size(10**26 * 30)
#> 3000.0 YB

Numbers

Ordinals:

count_as_ordinal(1)
#> [1] "1st"
count_as_ordinal(111)
#> [1] "111th"

Comma Seperation:

number_as_comma(1000)
#> [1] "1,000"
number_as_comma(10000)
#> [1] "10,000"

Words:

count_as_word(100)
#> [1] "100"
count_as_word(1000000)
#> [1] "1.0 million"
count_as_word(1200000000)
#> [1] "1.2 billion"

AP Format:

count_as_ap(3)
#> [1] "three"
count_as_ap(20)
#> [1] "20"

Todo

This is still a very early cut of the package.

  • Better support in times? For diff time?
  • Maybe add times relative to other times?
  • Export helper functions used in tests?