genvar: Generate one or more variables

View source: R/data-edit.R

genvarR Documentation

Generate one or more variables

Description

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.

Usage

genvar(
  ..., data = NULL, label = NULL, values = NULL, recode = NULL, ref = NULL,
  ordered = FALSE, times = NULL, each = NULL, fill = NA
)

Arguments

...

Named expressions in the form new_variable = expression. For backward compatibility, an explicit data frame may be supplied first.

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, genvar(smoking = c(1, 0), times = c(10, 10)) creates ten 1s followed by ten 0s without writing rep() manually. With several variables, use a named list.

each

Optional compact repetition. For example, genvar(group = c(1, 2, 3), each = 5) creates five observations per group.

fill

Value used to pad a generated variable when it is shorter than the current data. Default NA. If a new variable is longer than the data, existing columns are extended with missing values.

Value

The edited data frame invisibly.

Examples

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"))


R4VN documentation built on Sept. 30, 2026, 5:13 p.m.