map_data: Map Variables in a Data Frame to Specified Roles

View source: R/map_data.R

map_dataR Documentation

Map Variables in a Data Frame to Specified Roles

Description

Utility to map variables in a data.frame to specific roles, with options to trim variables not assigned to roles, to rename variables to match variable roles (vs. attaching a variable map as an attribute), and to clear the console with each step in variable mapping.

Usage

map_data(
  x,
  roles,
  defs,
  opt.roles = NULL,
  opt.defs = NULL,
  trim = TRUE,
  rename = TRUE,
  clear = TRUE
)

Arguments

x

A rectangular atomic data.frame whose variables are to be mapped.

roles

Unique string multivec containing labels for required variable roles (that are also valid object names without back-quoting).

defs

Unique string multivec with length(defs) == length(roles) containing definitions/descriptions associated with corresponding values of roles.

opt.roles

Optional unique string multivec containing labels for optional variable roles (that are also valid object names without back-quoting).

opt.defs

Optional unique string multivec with length(opt.defs) == length(opt.roles) containing definitions/descriptions associated with corresponding values of opt.roles.

trim, rename, clear

Logical scalar indicating, respectively, whether to trim un-mapped variables from x, whether to rename mapped variables using the associated values of roles and opt.roles, and whether to clear the console with each stop in the variable mapping process.

Details

Variable mapping is conducted interactively via the console for all variable roles in roles and opt.roles that do not have matching variable names in x. For variable roles in roles and opt.roles that have matching variable names in x, matching is done automatically.

Value

A rectangular atomic data.frame.

Examples

dg <- 1:9
ai <- letters[dg]
AI <- LETTERS[dg]
uf <- factor(ai)
of <- ordered(AI)
tf <- ai %in% c("a", "e", "i", "o", "u")

dtf <- data.frame(dg = dg, ai = ai, AI = AI, uf = uf, of = of, tf = tf, stringsAsFactors = F)

roles <- c("dg", "ai", "AI")
ROLES <- c("uf", "of", "tf")

defs  <- c("digits 1 through 9", "lowercase a-i" , "uppercase A-I"         )
DEFS  <- c("unordered factor"  , "ordered factor", "sampled logical values")

map_data(dtf, roles, defs)
map_data(dtf, roles, defs, trim = F)
map_data(dtf, roles, defs, opt.roles = ROLES, opt.defs = DEFS)

j-martineau/uj documentation built on Sept. 14, 2024, 4:40 a.m.