Skip to contents

Reorders the columns of x to match the variable order defined in the var_spec for dataset (by the order column if present, otherwise by row order). Columns not in the spec trail at the end with a warning. Equivalent to the order step inside apply_spec(), exposed for standalone use.

Usage

order_cols(x, spec, dataset)

Arguments

x

A data frame.

spec

A herald_spec object or a path to a spec file.

dataset

Character. The dataset name.

Value

The data frame with reordered columns (invisibly).

Examples

spec <- herald_spec(
  ds_spec = data.frame(dataset = "DM", label = "Demographics",
                       stringsAsFactors = FALSE),
  var_spec = data.frame(
    dataset  = "DM",
    variable = c("STUDYID", "USUBJID", "AGE"),
    order    = 1:3,
    stringsAsFactors = FALSE
  )
)
dm <- data.frame(AGE = 65L, USUBJID = "001", STUDYID = "S1",
                 stringsAsFactors = FALSE)
result <- order_cols(dm, spec, "DM")
names(result)  # "STUDYID" "USUBJID" "AGE"
#> [1] "STUDYID" "USUBJID" "AGE"