Convert values of temperature, volume, mass, area, and atmospheric pressure to different units
Convert values of temperature, volume, mass, area, and atmospheric pressure to different units
This is a basic function that converts values of temperature, volume, mass, area, and atmospheric pressure to different units. This can be useful in convert_DO(), convert_rate(), and convert_rate.ft() where some inputs must be in specific units (e.g. temperature in °C, atmospheric pressure in bar, area in m2). See Examples.
convert_val(x, from =NULL, to =NULL)
Arguments
x: numeric value or vector. Values to be converted to a different unit.
from: string. Unit of the original values.
to: string. Unit to be converted to. These defaults are applied if left NULL: volume "L", temperature "C", mass "kg", area "m2", pressure "bar".
Returns
Output is a numeric vector of converted values.
Details
Note the type of unit does not need to be specified. The function will automatically recognise it using the from unit.
If the 'to' input is left NULL, the following defaults are applied depending on the unit type of the from input:
volume: "L"
temperature: "C"
mass: "kg"
area: "m2"
pressure: "bar"
A fuzzy string matching algorithm is used to accept different unit formatting styles. For example, "msq""m2", "M2", "sqm" are all parsed as metres squared of area.
# Convert volumeconvert_val(10,"ml","L")convert_val(10:15,"ml","L")# Convert temperatureconvert_val(-273.15,"C","K")convert_val(-40,"C","F")convert_val(c(2,4,6,8),"C","F")# Convert pressureconvert_val(1,"atm","bar")convert_val(1010,"hpa","bar")convert_val(735,"torr","kpa")# Convert areaconvert_val(100,"cm2","m2")convert_val(10000,"mm2","cm2")# Convert massconvert_val(200,"g","kg")convert_val(10000,"ug","mg")# Use directly in a respR function which requires inputs to be# in a specific unit. For example, in convert_rate() pressure# must be in 'bar' and respirometer volume in 'L'.# Here, we know chamber volume is 200 ml, and pressure measured in mbar.x <- suppressWarnings(inspect(urchins.rd,1,2))rate <- calc_rate(x, from =20, to =30)convert_rate(rate, oxy.unit ="ml/l", time.unit ="min", output.unit ="mg/h", volume = convert_val(200,"ml","L"), S =35, t =15, P = convert_val(1010,"mbar","bar"))# Note, the default 'to' units are set to those respR requires in# these functions ('L' and 'bar' here), so do not necessarily need# to be specified:convert_rate(rate, oxy.unit ="ml/l", time.unit ="min", output.unit ="mg/h", volume = convert_val(200,"ml"), S =35, t =15, P = convert_val(1010,"mbar"))