| genvar | R Documentation |
Creates variables in an explicit data frame or the active R4VN data frame.
Logical expressions are stored as 1 and 0. If no active data exists,
genvar() can initialize a temporary active data frame from entered vectors.
A later vector may contain more observations than the current data; R4VN
expands the data frame and pads existing/shorter columns with missing values.
genvar(
..., data = NULL, label = NULL, values = NULL, recode = NULL, ref = NULL,
ordered = FALSE, times = NULL, each = NULL, fill = NA
)
... |
Named expressions in the form |
data |
Optional explicit data frame object. When omitted, active data is used. |
label |
A character label or one label per generated variable. |
values |
A common named value-label vector. |
recode |
An optional common named recode vector. |
ref |
Optional reference category. |
ordered |
Logical; create ordered factors. |
times |
Optional repetition counts. For one variable, |
each |
Optional compact repetition. For example, |
fill |
Value used to pad a generated variable when it is shorter than the current data. Default |
The edited data frame invisibly.
d <- data.frame(sbp = c(120, 150), dbp = c(75, 95))
usedf(d, quiet = TRUE)
genvar(hypertension = sbp >= 140 | dbp >= 90,
label = "Hypertension",
values = c("0" = "No", "1" = "Yes"))
# Extended usage examples
d <- data.frame(id = 1:4, age = c(17, 25, 40, 70),
sbp = c(118, 145, 132, 160),
dbp = c(75, 92, 80, 95), bmi = c(18, 22, 25, 30))
usedf(d, quiet = TRUE)
# Arithmetic expression
genvar(age_decade = age / 10, label = "Age in decades")
# Logical expression is stored as 0/1
genvar(hypertension = sbp >= 140 | dbp >= 90,
label = "Hypertension",
values = c("0" = "No", "1" = "Yes"), ref = "No")
# Generate several variables in one call
genvar(adult = age >= 18, overweight = bmi >= 23,
label = c("Adult", "Overweight or obesity"),
values = c("0" = "No", "1" = "Yes"))
# Generate and recode a grouped variable
genvar(age_group = age,
recode = c("min:17" = 1, "18:59" = 2, "60:max" = 3),
label = "Age group",
values = c("1" = "<18", "2" = "18-59", "3" = "60+"),
ordered = TRUE)
# Explicit-data syntax remains available
genvar(d, pulse_pressure = sbp - dbp, label = "Pulse pressure")
# Quick vector entry without creating a data frame first
genvar(weight = c(29, 26, 13, 23, 23, 25, 17, 22))
ghist(x = weight)
# A later variable may be longer; existing columns are padded with NA
genvar(age = c(81, 65, 89, 70, 87, 61, 94, 98, 81, 70))
# Compact repeated values
genvar(smoking = c(1, 0), times = c(10, 10),
values = c("0" = "No", "1" = "Yes"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.