Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting units in R

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?

like image 603
thequerist Avatar asked Aug 27 '11 13:08

thequerist


People also ask

Does R have a unit of measurement?

The units R package provides a new class, units , for numeric data with associated measurement units. Operations on objects of this class retain the unit metadata and provide automated dimensional analysis: dimensions are taken into consideration in computations and comparisons.

How do you convert lbs to kg in R?

There are approximately 2.2046 lbs in a kilogram, so divide the variable storing the weight in pounds by 2.2046 and store this value in a new variable for storing mass in kilograms.

How do you convert km to miles in R?

How to Convert Kilometers to Miles. The distance in miles is equal to the distance in kilometers multiplied by 0.621371. Since one kilometer is equal to 0.621371 miles, that's the conversion ratio used in the formula.

What does unit R mean?

R = roentgen. roentgen = 0.000258 A*s/kg.


2 Answers

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
like image 79
IRTFM Avatar answered Oct 10 '22 15:10

IRTFM


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).

like image 17
CephBirk Avatar answered Oct 10 '22 15:10

CephBirk