Add factors to data set based on spec information

ys_add_factors(
  .data,
  .spec,
  ...,
  .all = TRUE,
  .suffix = getOption("ys.fct.suffix", "_f")
)

yspec_add_factors(
  .data,
  .spec,
  ...,
  .all = TRUE,
  .suffix = getOption("ys.fct.suffix", "_f")
)

ys_make_factor(values, x, strict = TRUE)

yspec_make_factor(values, x, strict = TRUE)

Arguments

.data

the data set to modify

.spec

a yspec object

...

unquoted column names for modification

.all

if `TRUE` then any column with a `values` attribute or where the `make_factor` field evaluates to `TRUE` will be added as a factor

.suffix

used to make the column name for the factors

values

a vector of values to convert to a factor

x

a ycol object

strict

if `FALSE`, then an factor will be returned for any `values` type

Details

Note that `.suffix` can be chosen using option `ys.fct.suffix`.

Examples

spec <- load_spec_ex() ys_make_factor(c(1,0,1,1,1,0), spec$SEX)
#> [1] female male female female female male #> Levels: male female
data <- data.frame(SEX = c(1,1,1,1,0,0,1,1), STUDY= c(202,100)) head(ys_add_factors(data, spec, SEX, STUDY))
#> SEX STUDY SEX_f #> 1 1 202 female #> 2 1 100 female #> 3 1 202 female #> 4 1 100 female #> 5 0 202 male #> 6 0 100 male #> STUDY_f #> 1 The first phase 2 study conducted only at sites 1, 2, 3 when the formulation was still oral liquid only #> 2 The first phase 1 study #> 3 The first phase 2 study conducted only at sites 1, 2, 3 when the formulation was still oral liquid only #> 4 The first phase 1 study #> 5 The first phase 2 study conducted only at sites 1, 2, 3 when the formulation was still oral liquid only #> 6 The first phase 1 study
data <- ys_help$data() spec <- ys_help$spec() head(ys_add_factors(data, spec))
#> C NUM ID SUBJ TIME SEQ CMT EVID AMT DV AGE WT CRCL ALB BMI #> 1 NA 1 1 1 0.00 0 1 1 5 0.000 28.03 55.16 114.45 4.4 21.67 #> 2 NA 2 1 1 0.61 1 2 0 NA 61.005 28.03 55.16 114.45 4.4 21.67 #> 3 NA 3 1 1 1.15 1 2 0 NA 90.976 28.03 55.16 114.45 4.4 21.67 #> 4 NA 4 1 1 1.73 1 2 0 NA 122.210 28.03 55.16 114.45 4.4 21.67 #> 5 NA 5 1 1 2.15 1 2 0 NA 126.090 28.03 55.16 114.45 4.4 21.67 #> 6 NA 6 1 1 3.19 1 2 0 NA 84.682 28.03 55.16 114.45 4.4 21.67 #> AAG SCR AST ALT HT CP TAFD TAD LDOS MDV BLQ PHASE STUDY RF #> 1 106.36 1.14 11.88 12.66 159.55 0 0.00 0.00 5 1 0 1 1 norm #> 2 106.36 1.14 11.88 12.66 159.55 0 0.61 0.61 5 0 0 1 1 norm #> 3 106.36 1.14 11.88 12.66 159.55 0 1.15 1.15 5 0 0 1 1 norm #> 4 106.36 1.14 11.88 12.66 159.55 0 1.73 1.73 5 0 0 1 1 norm #> 5 106.36 1.14 11.88 12.66 159.55 0 2.15 2.15 5 0 0 1 1 norm #> 6 106.36 1.14 11.88 12.66 159.55 0 3.19 3.19 5 0 0 1 1 norm #> C_f SEQ_f EVID_f CP_f MDV_f BLQ_f PHASE_f STUDY_f RF_f #> 1 <NA> observation 1 normal 1 below Q 1 SAD Normal #> 2 <NA> dose 0 normal 0 below Q 1 SAD Normal #> 3 <NA> dose 0 normal 0 below Q 1 SAD Normal #> 4 <NA> dose 0 normal 0 below Q 1 SAD Normal #> 5 <NA> dose 0 normal 0 below Q 1 SAD Normal #> 6 <NA> dose 0 normal 0 below Q 1 SAD Normal