GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||
GeezerVison is showing journal.r-project.org — The R Journal: Measurement Units in R Measurement Units in RWe briefly review SI units, and discuss R packages that deal with measurement units, their compatibility and conversion. Built upon udunits2 and the UNIDATA udunits library, we introduce the package units that provides a class for maintaining unit metadata. When used in expression, it automatically converts units, and simplifies units of results when possible; in case of incompatible units, errors are raised. The class flexibly allows expansion beyond predefined units. Using units may eliminate a whole class of potential scientific programming mistakes. We discuss the potential and limitations of computing with explicit units.
Edzer Pebesma (Institute for Geoinformatics)
, Thomas Mailund (Bioinformatics Research Center, Aarhus University)
, James Hiebert (Pacific Climate Impacts Consortium, University of Victoria)
2016-12-12 1 IntroductionTwo quotes from (Cobb and Moore 1997) – “Data are not just numbers, they are numbers
with a context” and “in data analysis, context provides meaning” –
illustrate that for a data analysis to be meaningful, knowledge of the
data’s context is needed. Pragmatic aspects of this context include who
collected or generated the data, how this was done, and for which
purpose (Scheider et al. 2016); semantic aspects concern what the data represents:
which aspect of the world do the data refer to, when and where were they
measured, and what a value of R does allow for keeping some context with data, for instance
Furthermore, if spatial objects as defined in package sp (Pebesma and Bivand 2005) have a proper coordinate reference system set, they can be transformed to other datums, or converted to various flat (projected) representations of the Earth (Iliffe and Lott 2008). In many cases however, R drops contextual information. As an example, we look at annual global land-ocean temperature index1 since 1960: Here, the time difference units are reported for the the unit is dropped. Similarly, for linear regression coefficients we see where the unit of change is in degrees Celsius but either per day
( Most variables are somehow attributed with information about their
units, which specify what the value 1 of this variable represents.
This may be counts of something, e.g. 2 SIThe BIPM (Bureau International des Poids et Mesures) is the “the intergovernmental organization through which Member States act together on matters related to measurement science and measurement standards. Its recommended practical system of units of measurement is the International System of Units (Système International d’Unités, with the international abbreviation SI)2”. (International Bureau of Weights and Measures et al. 2001) describe the SI units, where, briefly, SI units
3 Related work in RSeveral R packages provide unit conversions. For instance,
measurements
(Birk 2016) provides a collection of tools to make working with
physical measurements easier. It converts between metric and imperial
units, or calculates a dimension’s unknown value from other dimensions’
measurements. It does this by the but uses for instance Function computes how many minutes it takes to travel 100 meters at 3 feet per second. Package NISTunits
(Gama 2014) provides fundamental physical constants (Quantity, Value,
Uncertainty, Unit) for SI and non-SI units, plus unit conversions, based
on the data from NIST (National Institute of Standards and Technology).
The package provides a single function for every unit conversion; all
but 5 from its 896 functions are of the form Both measurements and NISTunits are written entirely in R. 4 UNIDATA’s udunits library and the udunits2 R packageUdunits, developed by UCAR/UNIDATA, advertises itself on its web page4 as: “The udunits package supports units of physical quantities. Its C library provides for arithmetic manipulation of units and for conversion of numeric values between compatible units. The package contains an extensive unit database, which is in XML format and user-extendable. The R package udunits2 (Hiebert 2015) provides an R level interface to the most important functions in the C library. The functions provided by udunits2 are Dropping the
and Unlike the measurements and NISTunits, udunits2 parses units as expressions, and bases its logic upon the convertibility of expressions, rather than the comparison of fixed strings: This has the advantage that through complex computations, intermediate objects can have units that are arbitrarily complex, and that can potentially be simplified later on. It also means that the package practically supports an unlimited amount of derived units. 5 Udunits versus the Unified Code for Units of Measure (UCUM)Another set of encodings for measurement units is the Unified Code for Units of Measure (UCUM, (Schadow and McDonald 2009)). A dedicated web site5 describes the details of the differences between udunits and UCUM, and provides a conversion service between the two encoding sets. The UCUM website refers to some Java implementations, but some of the links seem to be dead. UCUM is the preferred encoding for standards from the Open Geospatial Consortium. udunits on the other hand is the units standard of choice by the climate science community, and is adopted by the CF (Climate and Forecast) conventions, which mostly uses NetCDF. NetCDF (Rew and Davis 1990) is a binary data format that is widely used for atmospheric and climate model predictions. The udunits library is a C library that has strong support from UNIDATA, and we decided to build our developments on this, rather than on Java implementations of UCUM with a less clear provenance. 6 Handling data with units in R: the units packageThe units package builds represents As an alternative to using Although one could attach Several manipulations with Manipulations that do not involve unit conversion are for instance addition: Explicit unit conversion is done by assigning new units: similar to the behaviour of When mixing units in sums, comparisons or concatenation, units are automatically converted to those of the first argument: where and meaningful error messages appear when units are not compatible: The full set of methods and method groups for where the method groups
When possible, new units are simplified: Units are printed as simple R expressions, e.g. Another way to print units commonly seen in Climate and Forecast
Conventions6 is However, The
mtcars, illustrating automatic units
in default axis labels (after conversion to SI) for base plot (left) and
ggplot (right); demo(ggforce) illustrates how these plots
are generated.
Automatic conversion between Objects of class 7 Discussion and conclusionsThe units R package provides a new class, Where the R packages measurements and NISTunits provide conversion between a fixed number of units, with the help of the udunits library and unit database R package units allows for arbitrarily complex derived units. By treating units as expressions it can derive, convert and simplify units. In addition, beyond the SI units packaged, units handles user-defined units not supported by udunits. Data in Despite all standardization efforts, units may still be ambiguous, or
subject to interpretation. For instance for the duration of one year
NISTunits or udunits2 give us an answer that depends on whether we
want a common, leap, Gregorian, Julian, tropical or siderial year
((Lang 2006), see also Future work includes extending packages that read external data from formats, databases or interfaces with support for measurement unit information into R, preserving the measurement unit information. Examples would be interfaces to HDF5 (e.g., h5, (Annau 2016)), RNetCDF (Michna and Woods 2016) or sos4R (Nüst et al. 2011). It would be nice to see units of measurements propagate into units of regression coefficient estimates. AcknowledgementsWe acknowledge three anonymous reviewers and the handling editor for their constructive comments, and Thomas Lin Pedersen for implementing the ggplot extensions in package ggforce that automatically add units to default ggplot axis labels (Fig 1). 7.1 CRAN packages usedlubridate, sp, measurements, NISTunits, udunits2, units, ggplot2, spacetime, h5, RNetCDF, sos4R 7.2 CRAN Task Views implied by cited packagesChemPhys, MissingData, NetworkAnalysis, Phylogenetics, ReproducibleResearch, Spatial, SpatioTemporal, TeachingStatistics, TimeSeries
M. Annau. h5: Interface to the ’HDF5’ library. 2016. URL https://CRAN.R-project.org/package=h5. R package version 0.9.7.
M. A. Birk. Measurements: Tools for units of measurement. 2016. URL https://CRAN.R-project.org/package=measurements. R package version 1.0.0.
G. W. Cobb and D. S. Moore. Mathematics, statistics, and teaching. American Mathematical Monthly, 801–823, 1997.
J. Gama. NISTunits: Fundamental physical constants and unit conversions from NIST. 2014. URL https://CRAN.R-project.org/package=NISTunits. R package version 1.0.0.
G. Grolemund and H. Wickham. Dates and times made easy with lubridate. Journal of Statistical Software, 40(1): 1–25, 2011. URL https://www.jstatsoft.org/index.php/jss/article/view/v040i03.
J. Hiebert. udunits2: Udunits-2 bindings for r. 2015. R package version 0.9.
J. Iliffe and R. Lott. Datums and map projections: For remote sensing, GIS and surveying. CRC Inc, 2008.
International Bureau of Weights and Measures, B. N. Taylor and A. Thompson. The international system of units (SI). 2001.
K. R. Lang. Astrophysical formulae volume II: Space, time, matter and cosmology, 3rd edition 1999. 2nd printing. Springer, 2006.
P. Michna and M. Woods. RNetCDF: Interface to NetCDF datasets. 2016. URL https://CRAN.R-project.org/package=RNetCDF. R package version 1.8-2.
D. Nüst, C. Stasch and E. J. Pebesma. Advancing geoinformation science for a changing world. Eds S. Geertman, W. Reinhardt and F. Toppen pages. 227–246 2011. Springer.
E. Pebesma. Spacetime: Spatio-temporal data in R. Journal of Statistical Software, 51(1): 1–30, 2012. URL https://www.jstatsoft.org/index.php/jss/article/view/v051i07.
E. J. Pebesma and R. S. Bivand. Classes and methods for spatial data in R. R News, 5: 9–13, 2005. URL http://cran.r-project.org/doc/Rnews/.
T. L. Pedersen. Ggforce: Accelerating ’ggplot2’. 2016. URL https://CRAN.R-project.org/package=ggforce. R package version 0.1.0.
R. Rew and G. Davis. NetCDF: An interface for scientific data access. IEEE computer graphics and applications, 10(4): 76–82, 1990.
J. A. Ryan and J. M. Ulrich. xts: eXtensible Time Series, . R package version 0.9-7, 2014. URL https://CRAN.R-project.org/package=xts.
G. Schadow and C. J. McDonald. The unified code for units of measure. Regenstrief Institute and UCUM Organization: Indianapolis, IN, USA, 2009.
S. Scheider, B. Gräler, E. Pebesma and C. Stasch. Modeling spatiotemporal information generation. International Journal of Geographical Information Science, 30(10): 1980–2008, 2016. URLhttp://dx.doi.org/10.1080/13658816.2016.1151520 .
H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2009. URL http://ggplot2.org.
H. Wickham, R. Francois and K. Müller. Tibble: Simple data frames. 2016. URL https://CRAN.R-project.org/package=tibble. R package version 1.2.
A. Zeileis and G. Grothendieck. Zoo: S3 infrastructure for regular and irregular time series. Journal of Statistical Software, 14(6): 1–27, 2005. DOI 10.18637/jss.v014.i06.
ReferencesReuseText and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...". CitationFor attribution, please cite this work as Pebesma, et al., "The R Journal: Measurement Units in R", The R Journal, 2016 BibTeX citation @article{RJ-2016-061,
author = {Pebesma, Edzer and Mailund, Thomas and Hiebert, James},
title = {The R Journal: Measurement Units in R},
journal = {The R Journal},
year = {2016},
note = {https://doi.org/10.32614/RJ-2016-061},
doi = {10.32614/RJ-2016-061},
volume = {8},
issue = {2},
issn = {2073-4859},
pages = {486-494}
}
© The R Foundation, web page contact. | ||||||||||||||||||||||||||||||||||||
| You are visitor 01402605 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |