42

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
thequerist
  • 1,774
  • 3
  • 19
  • 27
  • 1
    your_matrix * your_conversion_factor, no package necessary for this one. – Brandon Bertelsen Aug 27 '11 at 14:25
  • `RcppGSL` and `RcppArmadillo` are two R packages interfacing with external libraries that provide physical constants. – baptiste Aug 28 '11 at 06:24
  • @baptiste could you be more specific about the functions/features that these Rcpp* packages provide? – David LeBauer May 21 '13 at 18:31
  • @David it's in the docs for their respective web page: [Armadillo](http://arma.sourceforge.net/docs.html#constants), and [GSL](http://www.gnu.org/software/gsl/manual/gsl-ref.html#Physical-Constants). The Rcpp* packages provide access to those libraries from R. – baptiste May 21 '13 at 19:40
  • 1
    Converting physical units is tricky. I wrote a [unit conversion library in Ruby](https://www.github.com/boris-s/sy), so I know you have to separately treat dimensions, quantities and units, prefixes and abbreviations, preferred units for quantities etc. It's not simple. – Boris Stitnicky Aug 06 '16 at 03:52

5 Answers5

25

Edit: There is now an encyclopedic units package: NISTunits

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
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    This is a useful list. As usual I tried `library(sos); findFn("{unit conversion}")`. I thought that `ConvertMedUnits` answered the question before I realized it was specific to medical units ... – Ben Bolker Aug 27 '11 at 21:48
  • 2
    the `udunits2` package (and underlying software) is really powerful; on ubuntu, the package dependencies can be installed with `sudo apt-get install udunits-bin libudunits2-dev`. Then you can do things like `ud.convert(1, "miles/hr", "km/s")` and even `ud.convert(1, "Mg/ha/yr", "kg/m2/d")` and it has a large database of unit synonyms. – David LeBauer May 21 '13 at 17:01
  • 1
    I have since installed this package and as of 2013 I haven't needed any standalone applications to use this function. – AdamO Jun 25 '13 at 22:11
17

I know this is very late, but the package measurements has a function conv_unit() that may be what you're looking for. You enter the imperial value you have, what unit you're converting from (e.g. 'ft') and what you want to convert to (e.g. 'km'). It has a variety of different dimensions (not just length).

CephBirk
  • 6,422
  • 5
  • 56
  • 74
8

There is a "new" R package called units today, that was built upon the udunits2 R package, which is available a while ago.

Have a look to: https://cran.r-project.org/web/packages/units/vignettes/units.html#setting-units-unit-conversion

library(units)
(spd1 = 1:5 * with(ud_units, m/s))
##Units: m/s
##[1] 1 2 3 4 5

(spd2 = 1:5 * with(ud_units, km/h))
#Units: km/h
#[1] 1 2 3 4 5

spd1 + spd2                   # automatic conversion
#Units: m/s
#[1] 1.277778 2.555556 3.833333 5.111111 6.388889

spd1 * spd2                   # unit derivation
#Units: km*m/h/s
#[1]  1  4  9 16 25

spd1 * 10 * with(ud_units, s) # unit simplification
#Units: m
#[1] 10 20 30 40 50

spd1 + 10 * with(ud_units, s) # error checking
#Error in `units<-.units`(`*tmp*`, value = list(numerator = "m", denominator = "s")) : 
cannot convert s into m/s
Marco
  • 4,000
  • 3
  • 25
  • 26
  • 1
    This got even better: `spd2 = 1:5 %>% set_units(mile)`, then `spd2 %>% set_units(km)` results in: `Units: [km] [1] 1.609344 3.218688 4.828032 6.437376 8.046720` – Ufos Mar 14 '19 at 17:52
7

There is the unit() and convertUnit() functions in the grid package for specifying different length and dimension units. That may do what you want, or give you a place to start if not.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • looks like the [grid package](http://cran.r-project.org/web/packages/grid) has been archived – David LeBauer May 21 '13 at 01:46
  • 2
    @David, the `grid` package is no longer on CRAN because it is now one of the packages that installs with R, no need for a separate download from CRAN. – Greg Snow May 21 '13 at 16:04
  • @GregSnow is that so? I don't see `unit` or `convertUnit` without loading any packages – MichaelChirico Sep 11 '16 at 11:39
  • 1
    @MichaelChirico, there is a difference between installing a package and loading a package. Yes you still need to load the grid package to use unit and convertUnit, but you do not need to install the grid package separately from R. – Greg Snow Sep 12 '16 at 17:04
6

The udunits2 package does just that. It wraps the powerful UDUNITS library:

udunits2::ud.convert(1, "mi", "km")
## [1] 1.609344

On top of that, the units package (work in progress) aims at providing a type-safe system for doing arithmetics with units:

with(ud_units, 1 * mi + 2 * km)
## 2.242742 mi
with(ud_units, 100 * km / (2 * h))
## 50 km/h
with(ud_units, 1 * mi + 2 * lb)
## Error: cannot convert lb into mi
krlmlr
  • 25,056
  • 14
  • 120
  • 217