This function works like ys_add_factors() with the difference that the original columns become factors (retaining the original column names) and the original columns are retained with a suffix. You can think of this as a more convenient form of ys_add_factors(..., .suffix = "").

ys_factors(data, spec, ..., .keep_values = TRUE, .suffix = "_v")

Arguments

data

the data set to modify

spec

a yspec object

...

unquoted column names for modification; passing nothing through ... will signal for all columns to be considered for factors

.keep_values

logical; if TRUE, value columns will be retained with a .suffix.

.suffix

a suffix to be added to original columns (holding values).

Value

The original data frame is returned with columns converted to factors and (possibly) additional columns storing values.

Details

Factor conversion will only take place on source columns that aren't already factors. That is, if a column in data is already a factor, it will be ignored. This means the function can be called multiple times on the same input data, but once a column is converted to factor, it will cannot be converted again in subsequent calls.

See also

Examples


library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union

spec <- ys_help$spec()
data <- ys_help$data()

data <- ys_factors(data, spec)

head(data, 5)
#>      C NUM ID SUBJ TIME         SEQ CMT        EVID AMT      DV   AGE    WT
#> 1 <NA>   1  1    1 0.00 observation   1        dose   5   0.000 28.03 55.16
#> 2 <NA>   2  1    1 0.61        dose   2 observation  NA  61.005 28.03 55.16
#> 3 <NA>   3  1    1 1.15        dose   2 observation  NA  90.976 28.03 55.16
#> 4 <NA>   4  1    1 1.73        dose   2 observation  NA 122.210 28.03 55.16
#> 5 <NA>   5  1    1 2.15        dose   2 observation  NA 126.090 28.03 55.16
#>     CRCL ALB   BMI    AAG  SCR   AST   ALT     HT     CP TAFD  TAD LDOS
#> 1 114.45 4.4 21.67 106.36 1.14 11.88 12.66 159.55 normal 0.00 0.00    5
#> 2 114.45 4.4 21.67 106.36 1.14 11.88 12.66 159.55 normal 0.61 0.61    5
#> 3 114.45 4.4 21.67 106.36 1.14 11.88 12.66 159.55 normal 1.15 1.15    5
#> 4 114.45 4.4 21.67 106.36 1.14 11.88 12.66 159.55 normal 1.73 1.73    5
#> 5 114.45 4.4 21.67 106.36 1.14 11.88 12.66 159.55 normal 2.15 2.15    5
#>           MDV      BLQ PHASE STUDY     RF C_v SEQ_v EVID_v CP_v MDV_v BLQ_v
#> 1     missing above QL     1   SAD Normal  NA     0      1    0     1     0
#> 2 non-missing above QL     1   SAD Normal  NA     1      0    0     0     0
#> 3 non-missing above QL     1   SAD Normal  NA     1      0    0     0     0
#> 4 non-missing above QL     1   SAD Normal  NA     1      0    0     0     0
#> 5 non-missing above QL     1   SAD Normal  NA     1      0    0     0     0
#>   PHASE_v STUDY_v RF_v
#> 1       1       1 norm
#> 2       1       1 norm
#> 3       1       1 norm
#> 4       1       1 norm
#> 5       1       1 norm

spec$EVID
#>  name  value          
#>  col   EVID           
#>  type  numeric        
#>  short event ID       
#>  value 0 : observation
#>        1 : dose       

count(data, EVID, EVID_v)
#>          EVID EVID_v    n
#> 1 observation      0 3210
#> 2        dose      1 1150