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.
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"
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 centimetersor, " 3 ? in dollar",> 3 UK? = 4.4007 U.S. dollarsor "12 cubic meters to pints",> 12 (cubic meters) = 21 117.0391 Imperial pintsThat 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 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._____________________________ 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
Another issue with units is 'recognising' things. For example, Hz are also s^-1 ... S>>> Stavros Macrakis <macrakis at alum.mit.edu> 04/13/09 9:38 AM >>>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 thereto 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 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. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
>>> <Bill.Venables at csiro.au> 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}}