Converting units in R

thequerist picture thequerist · Aug 27, 2011 · Viewed 17.9k times · Source

I would like to convert from imperial units to metric and vice versa in R. How do I go about doing that?

If there is no current way of doing that, how can I create a package that would?

Answer

IRTFM picture IRTFM · Aug 27, 2011

The nature of the units may affect the presence or absence. In general I think, unfortunately, that @gsk3 is correct. There are some function in the 'Hmisc', 'gdata', and 'marelac' packages:

Setting a units attribute (for more than just time objects): http://finzi.psych.upenn.edu/R/library/Hmisc/html/units.html

Medical: http://finzi.psych.upenn.edu/R/library/gdata/html/ConvertMedUnits.html

'marelac' Index (mostly specialized to oceanography) http://finzi.psych.upenn.edu/R/library/marelac/html/00Index.html

Temperature: http://finzi.psych.upenn.edu/R/library/marelac/html/convert_T.html

Barometric: http://finzi.psych.upenn.edu/R/library/marelac/html/convert_p.html

Package "dielectric" was mentioned by @Brian Diggs in an answer to a similar question: Link to 'constants' help page in package 'dielectric'

I think those will provide examples of doing so and the function package.skeleton should help with the mechanics of package creation. I think the package would be a welcome addition.

Update: The 'udunits2' package was mentioned by Gabor Grothendieck in a similar question to rhelp July 23, 2012. It appears to require installation of a stand-alone OS-specific package of the same name. Without such installation you get a rather unhelpful error message:

> ud.convert(1, "miles", "km")  
Error in ud.convert(x, "miles", "km") : 
  Units miles and km are not convertible