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?
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).
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
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.
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
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
来源:https://stackoverflow.com/questions/7214781/converting-units-in-r