Unit conversions: Difference between revisions

From Opasnet
Jump to navigation Jump to search
(→‎Answer: prefixes and several units added)
 
(11 intermediate revisions by 3 users not shown)
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)]] (function convert.units).


:''The code related to this tool is now a part of [[Opasnet (R library)]].
'''Convert almost any unit to any other unit!
<rcode
showcode="-1"
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? (blank: basic SI units)|default:c('nmol', 'l')
"
>
out <- convert.units(amount, tounit, fromunit)
 
cat(paste(amount, fromunit, "=", out$Result, out$Unit, "\n"))
</rcode>


==Question==
==Question==
Line 10: Line 24:
==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
|}


<t2b index="From,To,Type,Observation" locations="Result,Description" unit="-">
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.
||Prefix|1|
 
 
<t2b index="Observation" locations="From,To,Type,Result,Description" unit="-">
||Prefix|1|basic unit
da||Prefix|10|deca
da||Prefix|10|deca
h||Prefix|100|hecto
h||Prefix|100|hecto
k||Prefix|1000|kilo
k||Prefix|1000|kilo
M||Prefix|1+e6|mega
M||Prefix|1e+6|mega
G||Prefix|1+e9|giga
G||Prefix|1e+9|giga
T||Prefix|1+e12|tera
T||Prefix|1e+12|tera
P||Prefix|1+e15|peta
P||Prefix|1e+15|peta
E||Prefix|1+e18|exa
E||Prefix|1e+18|exa
Z||Prefix|1+e21|zetta
Z||Prefix|1e+21|zetta
Y||Prefix|1+e24|yotta
Y||Prefix|1e+24|yotta
d||Prefix|0.1|deci
d||Prefix|0.1|deci
c||Prefix|0.01|centi
c||Prefix|0.01|centi
Line 35: Line 64:
z||Prefix|1e-21|zepto
z||Prefix|1e-21|zepto
y||Prefix|1e-24|yocto
y||Prefix|1e-24|yocto
J|J|Unit|1|Joule, the base unit for energy
J|J|Unit|1|Joule, the Si unit of energy
Wh|J|Unit|3600|1 Wh = 1 Wh * 3600 s/h = 3600 Ws = 3600 MJ
Wh|J|Unit|3600|1 Wh = 1 Wh * 3600 s/h = 3600 Ws = 3600 MJ
toe|J|Unit|3.5e+10|1 toe = 1000 kgoe = 1000 kg * 35 MJ/kg = 35000 MJ (energy as tons of oil equivalent)
toe|J|Unit|3.5e+10|1 toe = 1000 kgoe = 1000 kg * 35 MJ/kg = 35000 MJ (energy as tons of oil equivalent)
g|g|Unit|1|gram, the base unit for mass (although the definition is based on kg)
cal|J|Unit|4.1868|calorie
eV|J|Unit|1.602176565e-19|electronvolt
g|g|Unit|1|gram, the SI unit of mass (although the definition is based on kg)
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
s|s|Unit|1|second, the base unit for time
lb|g|Unit|453.59237|international avoirdupois pound
s|s|Unit|1|second, the SI unit of time
min|s|Unit|60|minute
min|s|Unit|60|minute
h|s|Unit|3600|hour
h|s|Unit|3600|hour
d|s|Unit|86400|day
d|s|Unit|86400|day
a|s|Unit|31557600|year (assuming 365.25 days per year)
a|s|Unit|31557600|year (assuming 365.25 days per year)
m|m|Unit|1|metre, the base unit of length
m|m|Unit|1|metre, the SI unit of length
in|m|Unit|0.0254|inch (strangely, some people still use these)
in|m|Unit|0.0254|inch (strangely, some people still use these)
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
Å|m|Unit|1e-10|1 angstrom = 100 pm
l|m3|Unit|1000|litre
m3|m3|Unit|1|cubic metre, the SI unit of volume
m^3|Unit|m3|1|
l|m3|Unit|0.001|litre
m^3|m3|Unit|1|
A|A|Unit|1|Ampere, the SI unit of electric current
V|V|Unit|1|Volt, the SI unit of electric potential
N|N|Unit|1|Newton, the SI unit of force
W|W|Unit|1|Watt, the SI unit of power
hp|W|Unit|735.498 75|horsepower (metric)
Pa|Pa|Unit|1|Pascal, the SI unit of pressure
psi|Pa|Unit|6894.757|pound-force per square inch
bar|Pa|Unit|100000|bar
atm|Pa|Unit|101325|atmosphere (standard)
mol|mol|Unit|1|mole, the SI unit of substance amount
M|mol /l|Unit|1|
Hz|Hz|Unit|1|Hertz, the SI unit of frequency
Hz|/s|Unit|1|
C|C|Unit|1|Coulomb, the SI unit of electric quantity
O|O|Unit|1|Ohm, the SI unit of resistance
F|F|Unit|1|Farad, the SI unit of capacitance
Wb|Wb|Unit|1|Weber, the SI unit of magnetic flux
-|%|Unit|0.01|Fraction
</t2b>
</t2b>


<rcode include="page:Opasnet_(R_library)|name:answer"
==Rationale==
variables="
 
name:amount|description:What is the amount?|
===Help===
name:fromunit|description:In what units is it?|type:text|
Start out by filling in the amount in the first field. In the second field write the original units from which you wish to convert. The third field is for the units you wish to convert the amount to. If you leave this blank, the amount will be converted to basic SI units.
name:tounit|description:To what unit do you want it?|type:text
">
out <- convert.units(amount, tounit, fromunit)


cat(paste(amount, fromunit, "=", out, tounit))
There are several things to notify when filling the second and third fields:
</rcode>
*Mark the units between '''apostrophies'''.
*Leave a space between different units.
*'''Do not''' leave a space after a division slash.


==Rationale==
Press ''Run code'' to execute the conversion tool. Wait a moment for it to complete the conversion.


==See also==
==See also==

Latest revision as of 08:09, 13 June 2012


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

Convert almost any unit to any other unit!

What is the amount?:

In what units is it?:

To what units do you want it? (blank: basic SI units):

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.


Unit conversions: Difference between revisions(-)
ObsFromToTypeResultDescription
1Prefix1basic unit
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 Si unit of 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)
26calJUnit4.1868calorie
27eVJUnit1.602176565e-19electronvolt
28ggUnit1gram, the SI unit of mass (although the definition is based on kg)
29tongUnit1e+6metric ton
30tngUnit1e+6metric ton
31lbgUnit453.59237international avoirdupois pound
32ssUnit1second, the SI unit of time
33minsUnit60minute
34hsUnit3600hour
35dsUnit86400day
36asUnit31557600year (assuming 365.25 days per year)
37mmUnit1metre, the SI unit of length
38inmUnit0.0254inch (strangely, some people still use these)
39ftmUnit0.3048foot = 12 inches
40mimUnit1609.344mile = 5280 feet
41ÅmUnit1e-101 angstrom = 100 pm
42m3m3Unit1cubic metre, the SI unit of volume
43lm3Unit0.001litre
44m^3m3Unit1
45AAUnit1Ampere, the SI unit of electric current
46VVUnit1Volt, the SI unit of electric potential
47NNUnit1Newton, the SI unit of force
48WWUnit1Watt, the SI unit of power
49hpWUnit735.498 75horsepower (metric)
50PaPaUnit1Pascal, the SI unit of pressure
51psiPaUnit6894.757pound-force per square inch
52barPaUnit100000bar
53atmPaUnit101325atmosphere (standard)
54molmolUnit1mole, the SI unit of substance amount
55Mmol /lUnit1
56HzHzUnit1Hertz, the SI unit of frequency
57Hz/sUnit1
58CCUnit1Coulomb, the SI unit of electric quantity
59OOUnit1Ohm, the SI unit of resistance
60FFUnit1Farad, the SI unit of capacitance
61WbWbUnit1Weber, the SI unit of magnetic flux
62-%Unit0.01Fraction

Rationale

Help

Start out by filling in the amount in the first field. In the second field write the original units from which you wish to convert. The third field is for the units you wish to convert the amount to. If you leave this blank, the amount will be converted to basic SI units.

There are several things to notify when filling the second and third fields:

  • Mark the units between apostrophies.
  • Leave a space between different units.
  • Do not leave a space after a division slash.

Press Run code to execute the conversion tool. Wait a moment for it to complete the conversion.

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>