Unit conversions: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
m (→Answer) |
||
Line 9: | Line 9: | ||
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. | ||
<t2b index="From,To,Observation" locations=" | <t2b index="From,To,Observation" locations="Result,Description" unit="-"> | ||
MWh|MJ|3600|1 MWh = 1 MWh * 3600 s/h = 3600 MWs = 3600 MJ | MWh|MJ|3600|1 MWh = 1 MWh * 3600 s/h = 3600 MWs = 3600 MJ | ||
toe|MJ|35000|1 toe = 1000 kgoe = 1000 kg * 35 MJ/kg = 35000 MJ | toe|MJ|35000|1 toe = 1000 kgoe = 1000 kg * 35 MJ/kg = 35000 MJ | ||
</t2b> | </t2b> | ||
<rcode> | <rcode | ||
############# | include="page:Opasnet_(R_library)|name:answer" | ||
> | |||
############# convert.units: a function that converts units from one to another, if possible. | |||
# value = a numeric vector with values to be converted | # value = a numeric vector with values to be converted | ||
# fromunit = a numeric vector with the current units | # fromunit = a numeric vector with the current units | ||
# tounit = a string for the new units to be used. Must be found from the To column from the table in [[Unit conversions]]. | # tounit = a string for the new units to be used. Must be found from the To column from the table in [[Unit conversions]]. | ||
# expo = exponent for the conversion. If -1 converts the inverse, i.e. backward, or a unit that is in the denominator of a compiled unit. | # expo = exponent for the conversion. If -1 converts the inverse, i.e. backward, or a unit that is in the denominator of a compiled unit. | ||
convert.units <- function(value, fromunit = NULL, tounit = NULL, expo = 1) { | |||
data <- | data <- fetch("Op_en5475") | ||
out <- merge(fromunit, data, by.y = | print(data) | ||
out <- out[out$To == tounit, ] | out <- merge(fromunit, data, by.y = "From") | ||
out <- merge(value, out, all.x = TRUE) | print(out) | ||
out <- out$value * out$Factor^expo | out <- out[out$To == tounit, ] | ||
return(out) | print(out) | ||
out <- merge(value, out, all.x = TRUE) | |||
print(out) | |||
out <- out$value * out$Factor^expo | |||
print(out) | |||
return(out) | |||
} | } | ||
cat("Starting model\n") | |||
a <- data.frame(Unit = "MW", Result = 1) | |||
convert.units(1, "MWh", "MJ") | |||
temp <- setGeneric("convert.units") | |||
temp <- setMethod( | |||
f = "convert.units", | |||
signature = signature(data = "ovariable"), | |||
definition = function(x, tounit = NULL) | |||
{ | |||
x@sample[c("Unit", "Result")] <- convert.units(x@sample$Result, x@sample$Unit, tounit) | |||
return(x) | |||
} | |||
) | |||
</rcode> | </rcode> | ||
{{attack|# |The code does not work because the merges and combinations do not match. Values should have some row identifier that makes it possible to link a row in value to the right row in out. Should value and fromunit be two columns in a data.frame?|--[[User:Jouni|Jouni]] 11:29, 25 January 2012 (EET)}} | {{attack|# |The code does not work because the merges and combinations do not match. Values should have some row identifier that makes it possible to link a row in value to the right row in out. Should value and fromunit be two columns in a data.frame?|--[[User:Jouni|Jouni]] 11:29, 25 January 2012 (EET)}} |
Revision as of 12:46, 31 May 2012
Moderator:Jouni (see all) |
This page is a stub. You may improve it into a full page. |
Upload data
|
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.
Obs | From | To | Result | Description |
---|---|---|---|---|
1 | MWh | MJ | 3600 | 1 MWh = 1 MWh * 3600 s/h = 3600 MWs = 3600 MJ |
2 | toe | MJ | 35000 | 1 toe = 1000 kgoe = 1000 kg * 35 MJ/kg = 35000 MJ |
⇤--#: . The code does not work because the merges and combinations do not match. Values should have some row identifier that makes it possible to link a row in value to the right row in out. Should value and fromunit be two columns in a data.frame? --Jouni 11:29, 25 January 2012 (EET) (type: truth; paradigms: science: attack)