keepvar: Keep variables and/or observations

View source: R/data-edit.R

keepvarR Documentation

Keep variables and/or observations

Description

Keeps variables or observations in an explicit data frame or active data.

Usage

keepvar(..., data = NULL, obs = NULL)

Arguments

...

Variable selectors. For backward compatibility, an explicit data frame may be the first unnamed argument.

data

Optional explicit data frame object.

obs

Optional observations to keep.

Value

The edited data frame invisibly.

Examples

d <- data.frame(
  id = 1:5,
  age = c(10, 20, NA, 40, 50),
  sex = c("M", "F", "F", "M", "F"),
  score_a = 1:5,
  score_b = 6:10
)

# Each explicit-data example uses its own copy because keepvar()
# intentionally edits the supplied object.
d1 <- d
keepvar(d1, id, age, sex)

d2 <- d
keepvar(d2, id:sex)

d3 <- d
keepvar(d3, id, "score_*")

d4 <- d
keepvar(d4, obs = age >= 18)

d5 <- d
keepvar(d5, id, age, obs = !missing(age))

d6 <- d
keepvar(d6, obs = c(1, 3, 5))

# Active-data syntax
active_d <- d
usedf(active_d, quiet = TRUE)
keepvar(id, age, obs = age >= 18)
usedf(clear = TRUE, quiet = TRUE)

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