Unit conversions: Difference between revisions

From Opasnet
Jump to navigation Jump to search
(more units)
Line 1: Line 1:
[[Category:R tool]]
[[Category:R tool]]
{{tool|moderator=Jouni|stub=Yes}}
{{method|moderator=Jouni|stub=Yes}}


:''The code related to this tool is now a part of [[Opasnet (R library)]].
:''The code related to this tool is now a part of [[Opasnet (R library)]].
Line 10: Line 10:
==Answer==
==Answer==


We need conversion factors, which are listed below, and a code that does the actual conversion.
We need conversion factors, which are listed below, and a code that does the actual conversion. Function ''convert.units'' is used to perform conversions. It has three parameters:
* x: a numeric vector of values that are to be converted.
* tounit: a character vector of units that are preferred in the output. This can contain more units than needed, as long as the the entries are exclusive (i.e., only one unit for length, only one for energy etc.).
* fromunit: a character vector of units used in the input. It has (or is coerced to) the same size as x.
 
Composite units can be used, e.g. ug /m3. Then, each part of the unit is converted separately, e.g. micrograms to picograms and cubic metre to litres.
> convert.units(1, tounit = c("pg", "l"), fromunit = "ug /m3")
will result in
{| {{prettytable}}
! Unit !!Result
|----
| pg /l||1000
|}
 
Note that in the units, different parts are separated with space, but the units in the denominator must have the slash before them without a space.
 
<rcode include="page:Opasnet_(R_library)|name:answer"
variables="
name:amount|description:What is the amount?|default:1|
name:fromunit|description:In what units is it?|default:'mol /m3'|
name:tounit|description:To what units do you want it?|default:c('nmol', 'l')
">
out <- convert.units(amount, tounit, fromunit)
 
cat(paste(amount, fromunit, "=", out$Result, out$Unit, "\n"))
</rcode>
 


<t2b index="From,To,Type,Observation" locations="Result,Description" unit="-">
<t2b index="From,To,Type,Observation" locations="Result,Description" unit="-">
Line 41: Line 67:
ton|g|Unit|1e+6|metric ton
ton|g|Unit|1e+6|metric ton
tn|g|Unit|1e+6|metric ton
tn|g|Unit|1e+6|metric ton
lb|g|Unit|453.59237|international avoirdupois pound
s|s|Unit|1|second, the base unit for time
s|s|Unit|1|second, the base unit for time
min|s|Unit|60|minute
min|s|Unit|60|minute
Line 50: Line 77:
ft|m|Unit|0.3048|foot = 12 inches
ft|m|Unit|0.3048|foot = 12 inches
mi|m|Unit|1609.344|mile = 5280 feet
mi|m|Unit|1609.344|mile = 5280 feet
m3|m3|Unit|1|cubic metre, the basic unit of colume
m3|m3|Unit|1|cubic metre, the basic unit of volume
l|m3|Unit|0.001|litre
l|m3|Unit|0.001|litre
m^3|m3|Unit|1|
m^3|m3|Unit|1|
A|A|Unit|1|Ampere, the basic unit of electric current
V|V|Unit|1|Volt, the basic unit of electric potential
N|N|Unit|1|Newton, the basic unit of force
Pa|Pa|Unit|1|Pascal, the basic unit of pressure
psi|Pa|Unit|6894.757|pound-force per square inch
bar|Pa|Unit|100000|bar
mol|mol|Unit|1|mole, the basic unit of substance amount
M|mol /l|Unit|1|
Hz|Hz|Unit|1|Hertz, the basic unit of frequency
Hz|/s|Unit|1|
C|C|Unit|1|Coulomb, the basic unit of electric quantity
</t2b>
</t2b>
<rcode include="page:Opasnet_(R_library)|name:answer"
variables="
name:amount|description:What is the amount?|
name:fromunit|description:In what units is it?|type:text|
name:tounit|description:To what unit do you want it?|type:text
">
out <- convert.units(amount, tounit, fromunit)
cat(paste(amount, fromunit, "=", out$Result, out$Unit, "\n"))
</rcode>


==Rationale==
==Rationale==

Revision as of 14:42, 3 June 2012



The code related to this tool is now a part of Opasnet (R library).

Question

How to convert data from one unit to another in R?

Answer

We need conversion factors, which are listed below, and a code that does the actual conversion. Function convert.units is used to perform conversions. It has three parameters:

  • x: a numeric vector of values that are to be converted.
  • tounit: a character vector of units that are preferred in the output. This can contain more units than needed, as long as the the entries are exclusive (i.e., only one unit for length, only one for energy etc.).
  • fromunit: a character vector of units used in the input. It has (or is coerced to) the same size as x.

Composite units can be used, e.g. ug /m3. Then, each part of the unit is converted separately, e.g. micrograms to picograms and cubic metre to litres.

> convert.units(1, tounit = c("pg", "l"), fromunit = "ug /m3")

will result in

Unit Result
pg /l 1000

Note that in the units, different parts are separated with space, but the units in the denominator must have the slash before them without a space.

What is the amount?:

In what units is it?:

To what units do you want it?:

+ Show code


Unit conversions: Difference between revisions(-)
ObsFromToTypeResultDescription
1Prefix1
2daPrefix10deca
3hPrefix100hecto
4kPrefix1000kilo
5MPrefix1e+6mega
6GPrefix1e+9giga
7TPrefix1e+12tera
8PPrefix1e+15peta
9EPrefix1e+18exa
10ZPrefix1e+21zetta
11YPrefix1e+24yotta
12dPrefix0.1deci
13cPrefix0.01centi
14mPrefix0.001milli
15μPrefix1e-6micro
16uPrefix1e-6micro
17nPrefix1e-9nano
18pPrefix1e-12pico
19fPrefix1e-15femto
20aPrefix1e-18atto
21zPrefix1e-21zepto
22yPrefix1e-24yocto
23JJUnit1Joule, the base unit for energy
24WhJUnit36001 Wh = 1 Wh * 3600 s/h = 3600 Ws = 3600 MJ
25toeJUnit3.5e+101 toe = 1000 kgoe = 1000 kg * 35 MJ/kg = 35000 MJ (energy as tons of oil equivalent)
26ggUnit1gram, the base unit for mass (although the definition is based on kg)
27tongUnit1e+6metric ton
28tngUnit1e+6metric ton
29lbgUnit453.59237international avoirdupois pound
30ssUnit1second, the base unit for time
31minsUnit60minute
32hsUnit3600hour
33dsUnit86400day
34asUnit31557600year (assuming 365.25 days per year)
35mmUnit1metre, the base unit of length
36inmUnit0.0254inch (strangely, some people still use these)
37ftmUnit0.3048foot = 12 inches
38mimUnit1609.344mile = 5280 feet
39m3m3Unit1cubic metre, the basic unit of volume
40lm3Unit0.001litre
41m^3m3Unit1
42AAUnit1Ampere, the basic unit of electric current
43VVUnit1Volt, the basic unit of electric potential
44NNUnit1Newton, the basic unit of force
45PaPaUnit1Pascal, the basic unit of pressure
46psiPaUnit6894.757pound-force per square inch
47barPaUnit100000bar
48molmolUnit1mole, the basic unit of substance amount
49Mmol /lUnit1
50HzHzUnit1Hertz, the basic unit of frequency
51Hz/sUnit1
52CCUnit1Coulomb, the basic unit of electric quantity

Rationale

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>