Get data from quantity objects including their values (equivalent of as.numeric) and text representations (equivalent of as.character).

get_qty_value(x, unit = NULL)

# S3 method for mk_quantity
as.double(x, unit = NULL, ...)

get_qty_text(x, unit = get_qty_units(x), signif = 5)

# S3 method for mk_quantity
as.character(x, unit = get_qty_units(x), signif = 5, ...)

get_qty_text_each(x, signif = 5)

Arguments

x

quantities

unit

which units to retrieve quantity data in (by default the unit the quantity is in)

...

additional parameters for the generics

signif

number of significant digits for printing the quantity

Functions

  • get_qty_value(): get the value of a quantity in the desired unit. By default returns the quantity in the units it is in. Functionally equivalent to as.numeric and as.double.

  • as.double(mk_quantity): S3 extension of as.numeric and as.double with optional unit argument

  • get_qty_text(): get the value of the quantity in the desired unit as a text string with the unit appended. Functionally equivalent to as.character.

  • as.character(mk_quantity): S3 implementation of as.character with optional unit and signif argument

  • get_qty_text_each(): get each value of a quantity in the best metric unit with the unit appended. Note that if a value is zero, it will use the unit of the next smallest value for this number.

Examples

# quantity value examples
qty(0.1, "g") %>% get_qty_value()
#> [1] 100
qty(0.1, "g") %>% get_qty_value("g")
#> [1] 0.1
qty(0, "C") %>% get_qty_value("F")
#> [1] 32
qty(760, "Torr") %>% get_qty_value("atm")
#> [1] 1
qty(760, "Torr") %>% as.numeric("atm")
#> [1] 1

# quantity text examples
qty(0.1, "g") %>% get_qty_text()
#> [1] "100 mg"
qty(0.1, "g") %>% get_qty_text("g")
#> [1] "0.1 g"
qty(0:10, "C") %>% get_qty_text("F")
#>  [1] "32 F"   "33.8 F" "35.6 F" "37.4 F" "39.2 F" "41 F"   "42.8 F" "44.6 F"
#>  [9] "46.4 F" "48.2 F" "50 F"  
qty(760, "Torr") %>% get_qty_text("atm")
#> [1] "1 atm"
qty(760, "Torr") %>% as.character("atm")
#> [1] "1 atm"

# each quantity text example
qty(c(0, 0.1, 10, 1000), "mg") %>% get_qty_text() # 0.1mg 10mg 1000mg
#> [1] "0 mg"    "0.1 mg"  "10 mg"   "1000 mg"
qty(c(0, 0.1, 10, 1000), "mg") %>% get_qty_text_each() # 0ug 100ug 10mg 1g
#> [1] "0 µg"   "100 µg" "10 mg"  "1 g"