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 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).
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
library(sos); findFn("{unit conversion}")
. I thought that ConvertMedUnits
answered the question before I realized it was specific to medical units ... –
Frulla 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 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).
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
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
–
Lx 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.
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 unit
or convertUnit
without loading any packages –
Saluki 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
© 2022 - 2024 — McMap. All rights reserved.
RcppGSL
andRcppArmadillo
are two R packages interfacing with external libraries that provide physical constants. – Talbot