[R] Physical Units in Calculations

2009-04-12 Thread Tom La Bone

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? 

Tom
-- 
View this message in context: 
http://www.nabble.com/Physical-Units-in-Calculations-tp23016092p23016092.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Physical Units in Calculations

2009-04-12 Thread Robert A LaBudde

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: r...@lcfltd.com
Least Cost Formulations, Ltd.URL: http://lcfltd.com/
824 Timberlake Drive Tel: 757-467-0954
Virginia Beach, VA 23464-3239Fax: 757-467-2947

"Vere scire est per causas scire"

__
R-help@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.


Re: [R] Physical Units in Calculations

2009-04-12 Thread Bill.Venables
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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Robert A LaBudde
Sent: Monday, 13 April 2009 12:20 PM
To: Tom La Bone
Cc: r-help@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: r...@lcfltd.com
Least Cost Formulations, Ltd.URL: http://lcfltd.com/
824 Timberlake Drive Tel: 757-467-0954
Virginia Beach, VA 23464-3239Fax: 757-467-2947

"Vere scire est per causas scire"

__
R-help@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.

__
R-help@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.


Re: [R] Physical Units in Calculations

2009-04-13 Thread Stavros Macrakis
On Sun, Apr 12, 2009 at 11:01 PM,   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

__
R-help@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.


Re: [R] Physical Units in Calculations

2009-04-13 Thread baptiste auguie

Hi,


Two thoughts I'd like to share on this subject:


1) Something really cool for conversions between units is the Google  
search bar: type in " 3 inches in cm" and you get,



3 inches = 7.62 centimeters


or, " 3 £ in dollar",


3 UK£ = 4.4007 U.S. dollars


or "12 cubic meters to pints",


12 (cubic meters) = 21 117.0391 Imperial pints


That is a very clever pattern recognition, and very useful for this  
task.


I think a similar implementation would be great to facilitate the use  
of units within R, I'm not sure Google has shared the code though.



2) the Grid package provides the unit() functions that handles  
conversions between different systems (inches, cm, pts, ...). I'm sure  
Paul Murrell thought a lot about the best implementation for this (I'm  
guessing he is the author of that function),



getAnywhere(valid.unit)



function (x, units, data)
{
valid.units <- valid.units(units)
data <- valid.data(rep(units, length.out = length(x)), data)
attr(x, "unit") <- units
attr(x, "valid.unit") <- valid.units
attr(x, "data") <- data
class(x) <- "unit"
x
}




so we could imagine these two functions,


convert(12, from= "m^3", to = "pint", round=FALSE)



or,


unit(12, "m^3") + unit(3, "pint")




Note that lengths (perhaps also time and color to a minor extent) are  
especially important in that it's the only actual physical dimension  
rendered in a final plot, the others are put in correspondence  
(isomorphism between continuous variables) with the labels set  
accordingly.


Best wishes,

baptiste




On 13 Apr 2009, at 01:00, 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?

Tom
--
View this message in context: 
http://www.nabble.com/Physical-Units-in-Calculations-tp23016092p23016092.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


_

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

__
R-help@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.


Re: [R] Physical Units in Calculations

2009-04-13 Thread S Ellison
Another issue with units is 'recognising' things. For example, Hz are
also s^-1 ...

S

>>> Stavros Macrakis  04/13/09 9:38 AM >>>
On Sun, Apr 12, 2009 at 11:01 PM,   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

__
R-help@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.


***
This email and any attachments are confidential. Any use...{{dropped:8}}

__
R-help@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.


Re: [R] Physical Units in Calculations

2009-04-13 Thread S Ellison
>>>  04/13/09 4:01 AM >>>
>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.
No, we don't. We use miles per (Imperial) gallon. 
Only Euronuts would combine litres and imperial units.

S


***
This email and any attachments are confidential. Any use...{{dropped:8}}

__
R-help@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.


Re: [R] Physical Units in Calculations

2009-04-14 Thread Patrick Connolly
On Mon, 13-Apr-2009 at 09:15PM +0100, S Ellison wrote:

|> >>>  04/13/09 4:01 AM >>>
|> >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.
|> No, we don't. We use miles per (Imperial) gallon. 

But petrol is priced by the litre.



-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___Patrick Connolly   
 {~._.~}   Great minds discuss ideas
 _( Y )_ Average minds discuss events 
(:_~*~_:)  Small minds discuss people  
 (_)-(_)  . Eleanor Roosevelt
  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

__
R-help@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.