[R] Physical Units in Calculations

Stavros Macrakis macrakis at alum.mit.edu
Mon Apr 13 10:38:45 CEST 2009


On Sun, Apr 12, 2009 at 11:01 PM,  <Bill.Venables at csiro.au> wrote:
> It is, however, an interesting problem and there are the tools there to handle it.  Basically you need to create a class for each kind of measure you want to handle ("length", "area", "volume", "weight", and so on) and then overload the arithmetic operators so that they can handle arguments of the appropriate class.

I'd think it would be far simpler and cleaner to have a single
dimensioned-units class with a slot for magnitude and one for the
power of each dimension -- M, L, T are uncontroversial, pick your
system for electromagnetism and thermodynamics....  Once you have
that, you have not just mass, length, and time, but also area, volume,
density, acceleration, viscosity, etc. etc.

It would of course be nice if the existing difftime class could be fit
into this, as it is currently pretty much a second-class citizen.  For
example, c of two time differences is currently a numeric vector,
losing its units (hours, days, etc.) completely.

One of the difficulties of adding units would be, I suspect, making
them work nicely with the rest of the system.  For example, although
sum is defined abstractly in terms of '+', as far as I can tell
sum.units would have to be overloaded explicitly. Similarly for mean,
cumsum, rle, var, %*%, etc. etc.

              -s




More information about the R-help mailing list