Learn R Programming

humanize

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?

Copy Link

Version

Install

install.packages('humanize')

Monthly Downloads

352

Version

0.2.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Gerry Manoim

Last Published

April 4th, 2018

Functions in humanize (0.2.0)

seconds_to_natural_delta

Takes in a number of seconds and computes a "human" delta
count_as_word

Convert Large Counts into Friendly Text
natural_time

Convert times to natural values relative to now.
natural_date

Natural Date
reexports

Objects exported from other packages
count_as_ordinal

Transform a count to an ordinal string
number_as_comma

Convert an number to a string with comma separation
count_as_ap

Convert to AP Number
natural_size

Convert bytes to a more natural representation
natural_day

Natural Day