Unit conversions: Difference between revisions
Jump to navigation
Jump to search
m (→Answer) |
(more units) |
||
Line 1: | Line 1: | ||
[[Category:R tool]] | [[Category:R tool]] | ||
{{ | {{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 | 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> | ||
==Rationale== | ==Rationale== |
Revision as of 14:42, 3 June 2012
Moderator:Jouni (see all) |
This page is a stub. You may improve it into a full page. |
Upload data
|
- 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.
Obs | From | To | Type | Result | Description |
---|---|---|---|---|---|
1 | Prefix | 1 | |||
2 | da | Prefix | 10 | deca | |
3 | h | Prefix | 100 | hecto | |
4 | k | Prefix | 1000 | kilo | |
5 | M | Prefix | 1e+6 | mega | |
6 | G | Prefix | 1e+9 | giga | |
7 | T | Prefix | 1e+12 | tera | |
8 | P | Prefix | 1e+15 | peta | |
9 | E | Prefix | 1e+18 | exa | |
10 | Z | Prefix | 1e+21 | zetta | |
11 | Y | Prefix | 1e+24 | yotta | |
12 | d | Prefix | 0.1 | deci | |
13 | c | Prefix | 0.01 | centi | |
14 | m | Prefix | 0.001 | milli | |
15 | μ | Prefix | 1e-6 | micro | |
16 | u | Prefix | 1e-6 | micro | |
17 | n | Prefix | 1e-9 | nano | |
18 | p | Prefix | 1e-12 | pico | |
19 | f | Prefix | 1e-15 | femto | |
20 | a | Prefix | 1e-18 | atto | |
21 | z | Prefix | 1e-21 | zepto | |
22 | y | Prefix | 1e-24 | yocto | |
23 | J | J | Unit | 1 | Joule, the base unit for energy |
24 | Wh | J | Unit | 3600 | 1 Wh = 1 Wh * 3600 s/h = 3600 Ws = 3600 MJ |
25 | toe | J | Unit | 3.5e+10 | 1 toe = 1000 kgoe = 1000 kg * 35 MJ/kg = 35000 MJ (energy as tons of oil equivalent) |
26 | g | g | Unit | 1 | gram, the base unit for mass (although the definition is based on kg) |
27 | ton | g | Unit | 1e+6 | metric ton |
28 | tn | g | Unit | 1e+6 | metric ton |
29 | lb | g | Unit | 453.59237 | international avoirdupois pound |
30 | s | s | Unit | 1 | second, the base unit for time |
31 | min | s | Unit | 60 | minute |
32 | h | s | Unit | 3600 | hour |
33 | d | s | Unit | 86400 | day |
34 | a | s | Unit | 31557600 | year (assuming 365.25 days per year) |
35 | m | m | Unit | 1 | metre, the base unit of length |
36 | in | m | Unit | 0.0254 | inch (strangely, some people still use these) |
37 | ft | m | Unit | 0.3048 | foot = 12 inches |
38 | mi | m | Unit | 1609.344 | mile = 5280 feet |
39 | m3 | m3 | Unit | 1 | cubic metre, the basic unit of volume |
40 | l | m3 | Unit | 0.001 | litre |
41 | m^3 | m3 | Unit | 1 | |
42 | A | A | Unit | 1 | Ampere, the basic unit of electric current |
43 | V | V | Unit | 1 | Volt, the basic unit of electric potential |
44 | N | N | Unit | 1 | Newton, the basic unit of force |
45 | Pa | Pa | Unit | 1 | Pascal, the basic unit of pressure |
46 | psi | Pa | Unit | 6894.757 | pound-force per square inch |
47 | bar | Pa | Unit | 100000 | bar |
48 | mol | mol | Unit | 1 | mole, the basic unit of substance amount |
49 | M | mol /l | Unit | 1 | |
50 | Hz | Hz | Unit | 1 | Hertz, the basic unit of frequency |
51 | Hz | /s | Unit | 1 | |
52 | C | C | Unit | 1 | Coulomb, the basic unit of electric quantity |
Rationale
See also
References
Related files
<mfanonymousfilelist></mfanonymousfilelist>