Converting units in R
Asked Answered
M

5

43

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?

Mathieu answered 27/8, 2011 at 13:14 Comment(5)
your_matrix * your_conversion_factor, no package necessary for this one.Mandal
RcppGSL and RcppArmadillo are two R packages interfacing with external libraries that provide physical constants.Talbot
@Talbot could you be more specific about the functions/features that these Rcpp* packages provide?Rau
@David it's in the docs for their respective web page: Armadillo, and GSL. The Rcpp* packages provide access to those libraries from R.Talbot
Converting physical units is tricky. I wrote a unit conversion library in Ruby, so I know you have to separately treat dimensions, quantities and units, prefixes and abbreviations, preferred units for quantities etc. It's not simple.Balalaika
A
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).

Alie answered 29/8, 2014 at 14:17 Comment(0)
M
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
Micco answered 27/8, 2011 at 15:18 Comment(3)
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 ...Frulla
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.Rau
I have since installed this package and as of 2013 I haven't needed any standalone applications to use this function.Rolfe
A
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).

Alie answered 29/8, 2014 at 14:17 Comment(0)
D
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
Dubbing answered 10/2, 2017 at 14:8 Comment(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.046720Lx
R
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.

Roughhouse answered 27/8, 2011 at 16:22 Comment(4)
looks like the grid package has been archivedRau
@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.Roughhouse
@GregSnow is that so? I don't see unit or convertUnit without loading any packagesSaluki
@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.Roughhouse
C
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
Champac answered 13/9, 2016 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.