Format numbers (with uncertainty) according to the "Guide to the Expression of Uncertainty in Measurement (GUM)"

Uncertainties are rounded by default to two significant digits. Then, the values are rounded to the precision implied by the uncertainty. If the uncertainty is missing, values are rounded to two significant digits by default. The number of digits and the justification of the output format can be changed.

format_GUM(
  value,
  se = NULL,
  digits = 2,
  justify = c("decimal", "left", "right", "centre"),
  na.string = "--",
  ...
)

Arguments

value

numeric values to be formatted.

se

uncertainty values for value, either absolute (e.g. 0.93) or relative (e.g. "12%"). Defaults to NULL, in which case value is rounded according to digits.

digits

number of significant digits to which se or value (if se is missing) is rounded.

justify

character (decimal, left, right or centre) to determine the output format if length(value) > 1.

na.string

instead of NA write na.string.

...

additional arguments going to base format.

Value

Character vector of rounded and justified values.

References

ISO, IEC, and BIPM OIML. Guide to the Expression of Uncertainty in Measurement. Geneva, Switzerland 1995.

Author

Daniel Kaschek, IntiQuan

Examples

value <- c(2.0873624, 0.0057687, 0.087865, 3856.9898273, 1736.8735123)
se    <- c(0.0013756, 0.001765, 0.0006782, 9.23958, 124.79823)

niceTable <- data.frame(
  Value  = format_GUM(value, se),
  StdErr = format_GUM(se)
)

print(niceTable)
#>        Value    StdErr
#> 1    2.0874    0.0014 
#> 2    0.0058    0.0018 
#> 3    0.08786   0.00068
#> 4 3857.0       9.2    
#> 5 1740       120      

# Another example assuming 10% uncertainty for all values
format_GUM(value, se = "10%")
#> [1] "   2.09   " "   0.00577" "   0.0879 " "3860      " "1740      "