ordervar: Reorder variables

View source: R/data-edit.R

ordervarR Documentation

Reorder variables

Description

Reorders variables in an explicit data frame or the active R4VN data frame.

Usage

ordervar(..., data = NULL, before = NULL, after = NULL, last = FALSE)

Arguments

...

Variables to move. For backward compatibility, an explicit data frame may be the first unnamed argument.

data

Optional explicit data frame object.

before

Optional anchor variable.

after

Optional anchor variable.

last

Logical; move variables to the end.

Value

The edited data frame invisibly.

Examples

d <- data.frame(age = 1, sex = 2, id = 3, bmi = 4)
usedf(d, quiet = TRUE)
ordervar(id, sex)
ordervar(bmi, after = age)

# Extended usage examples

d <- data.frame(age = 1, sex = 2, id = 3, bmi = 4, outcome = 5)

# Move variables to the beginning
d1 <- d; ordervar(d1, id, outcome)

# Move before or after an anchor
d2 <- d; ordervar(d2, outcome, before = age)
d3 <- d; ordervar(d3, bmi, after = age)

# Move to the end
d4 <- d; ordervar(d4, id, last = TRUE)

# Use a range or wildcard
d5 <- data.frame(id = 1, q1 = 2, q2 = 3, q3 = 4, age = 5)
ordervar(d5, q1:q3, last = TRUE)

# Active-data syntax
usedf(d, quiet = TRUE); ordervar(id, outcome)


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