Get quantity information

get_qty_value(q, unit = get_qty_units(q), transform = identity)

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

get_qty_units(q)

get_qty_units_with_label(q, label = names(q))

Arguments

q

quantity or list of quantities

unit

the unit of the quantity

transform

whether to transform the value with an additional function once in the desired units. Common transformation examples are log10 and log (natural log) but custom transformations are also possible. Default is NO transformation (identity).

signif

number of significant digits for printing the quantity

label

text label to use with the units - single value or vector of the same length as q. By default uses the names of q, which only works if q is a list or data frame.

Details

get_qty_value: get the value of a quantity in the desired unit. By default returns the quantity in the units it is in.

get_qty_text: get the value of the quantity in the desired unit as a text string with the unit appended

get_qty_units: get units from a quantity, list of quantities or data frame (returns NA for objects/columns that are not quantities)

get_qty_units_with_label get units from a quantity, list of quantities or data frame, with a custom label in the format label [units]. Objects/columns that are not quantities simply return the label with out the [units] part.

See also

Other quantity functions: metric, quantities

Examples

qty(0.1, "g") %>% get_qty_value()
#> [1] 100
qty(0.1, "g") %>% get_qty_value("g")
#> [1] 0.1
qty(0.1, "g") %>% get_qty_value("g", log10)
#> [1] -1
qty(0, "C") %>% get_qty_value("F")
#> [1] 32
qty(760, "Torr") %>% get_qty_value("atm")
#> [1] 1
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(5000, "g") %>% get_qty_units()
#> [1] "kg"
x <- list(a = qty(5000, "g"), b = 42, c = qty(100, "mbar")) x %>% get_qty_units()
#> a b c #> "kg" NA "mbar"
# labels with units get_qty_units_with_label(qty(0.1, "mM"), "concentration")
#> [1] "concentration [µM]"
# make labels with units for data frame columns x <- data.frame(a = qty(1, "mg"), b = 2, c = qty(100, "mbar")) get_qty_units_with_label(x)
#> a b c #> "a [mg]" "b" "c [mbar]"
get_qty_units_with_label(x, "same label")
#> a b c #> "same label [mg]" "same label" "same label [mbar]"