[R] Physical Units in Calculations

Bill.Venables at csiro.au Bill.Venables at csiro.au
Mon Apr 13 05:01:23 CEST 2009


Is there anything available "off the shelf" in R for this?  I don't think so.

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.  This may be a case where S4 classes do have a distinct advantage, as they can more easily dispatch methods on combinations of classes rather than the class of a single argument, as in the case of S3.

It looks like an interesting problem, in fact, but with the potential to get well out of hand if you set your sights too general.  Limited versions would be simple enough, though.  Something like it exists for times in the POSIXt and Date classes, of course, but with many limitations.  For example you cannot divide one time difference by another to get a pure number, but you can divide one by a pure number to get another time difference.  This could be remedied, of course.

I was about the say that this is another case where the USA is mainly to blame, *again*, because of the dogged clinging to an outdated system of weights and measures, (not to mention the peverse practice putting the month *first* in their date format), but it's not entirely true.   The UK uses metres for most lengths but miles for road distances - the worst of all worlds.  They even measure fuel performance in litres per 100 *miles*, if you can believe it.


Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Robert A LaBudde
Sent: Monday, 13 April 2009 12:20 PM
To: Tom La Bone
Cc: r-help at r-project.org
Subject: Re: [R] Physical Units in Calculations

At 08:00 PM 4/12/2009, Tom La Bone wrote:

>Back in 2005 I had been doing most of my work in Mathcad for over 10 years.
>For a number of reasons I decided to switch over to R. After much effort and
>help from you folks I am finally "thinking" in R rather than thinking in
>Mathcad and trying to translating to R. Anyway, the only task I still use
>Mathcad for is calculations that involve physical quantities and units. For
>example, in Mathcad I can add 1 kilometer to 1 mile and get the right answer
>in the units of length I choose. Likewise, if I try to add 1 kilometer to 1
>kilogram I get properly chastised. Is there a way in R to assign quantities
>and units to numbers and have R keep track of them like Mathcad does?

Yes, but it's a lot of work: Create objects with units as an attribute.

Perhaps someone else can tell you if such a set of definitions already exists.

================================================================
Robert A. LaBudde, PhD, PAS, Dpl. ACAFS  e-mail: ral at lcfltd.com
Least Cost Formulations, Ltd.            URL: http://lcfltd.com/
824 Timberlake Drive                     Tel: 757-467-0954
Virginia Beach, VA 23464-3239            Fax: 757-467-2947

"Vere scire est per causas scire"

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list