View source: R/calc_addVariable.R
calc_addVariable | R Documentation |
Calculate new variables from existing ones, using generic formulas.
calc_addVariable(
data,
...,
units = NA,
na.rm = TRUE,
completeMissing = FALSE,
only.new = FALSE,
variable = variable,
unit = unit,
value = value,
overwrite = TRUE,
skip.missing.rhs = FALSE
)
calc_addVariable_(
data,
.dots,
na.rm = TRUE,
completeMissing = FALSE,
only.new = FALSE,
variable = "variable",
unit = "unit",
value = "value",
overwrite = TRUE,
skip.missing.rhs = FALSE
)
data |
A data frame. |
... |
Name-value pairs of calculation formulas. See details. |
units |
Character vector of units corresponding to new variables. Must
be of length equal to |
na.rm |
If |
completeMissing |
If |
only.new |
If |
variable |
Column name of variables. Defaults to |
unit |
Column name of units. Defaults to |
value |
Column name of values. Defaults to |
overwrite |
If |
skip.missing.rhs |
If |
.dots |
Used to work around non-standard evaluation. See details. |
...
is a list of name-value pairs with the general format
"lhs" = "rhs + calculations - formula", "`lhs 2`" = "lhs / `rhs 2`"
where lhs
are the names of new variables to be calculated and
rhs
are the variables to calculate from. If lhs
and rhs
are no proper identifiers, they need to be quoted (see
Quotes for details). When in doubt, just quote.
If the new variables should have units, set units
appropriately.
.dots
is a named list of strings denoting formulas and optionally
units. The general format is
list("`lhs 1`" = "`rhs` / `calculation`", "`lhs 2`" = "sin(`rhs 2`)")
Units are optionally included with the formulas in a vector like
list("`lhs w/ unit`" = c("`rhs 1` + `rhs 2`", "rhs unit")
Units do not require quoting.
...
and .dots
are processed in order, and variables already
calculated in the same call can be used for further calculations. Other
existing columns, including period
, can be referenced, but this is
not supported and the results are considered undefined.
A data frame.
Michaja Pehl
data <- inline.data.frame(c(
"model; scenario; region; variable; unit; period; value",
"REMIND; Baseline; USA; GDP|MER; billion US$2005/yr; 2010; 12990",
"REMIND; Baseline; USA; Population; million; 2010; 310.4",
"REMIND; Baseline; USA; PE; EJ/yr; 2010; 91.62",
"REMIND; Baseline; CHN; GDP|MER; billion US$2005/yr; 2020; 8882",
"REMIND; Baseline; CHN; GDP|MER; billion US$2005/yr; 2010; 4119",
"REMIND; Baseline; CHN; Population; million; 2020; 1387",
"REMIND; Baseline; CHN; Population; million; 2010; 1349"))
calc_addVariable(data, "GDPpC" = "`GDP|MER` / Population * 1e3",
"`ln GDPpC`" = "log(GDPpC)",
units = c("US$2005/cap", NA))
calc_addVariable_(
data,
list("`GDPpC`" = c("`GDP|MER` / `Population` * 1e3", "US$/cap"),
"`ln GDPpC`" = "log(`GDPpC`)")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.