Skip to contents

Merge variables in a data frame such as returned by dbGetFieldsIntoDf into a new variable, and optionally also map its values to new levels.

Usage

dfMergeVariablesRelevel(df = NULL, colnames = "", levelslist = NULL)

Arguments

df

A data.frame with the variables (columns) to be merged into one vector.

colnames

A vector of names of columns in `df` that hold the variables to be merged, or a selection of columns as per select.

levelslist

A names list with one slice each for a new value to be used for a vector of old values (optional).

Value

A vector, with the type of the columns to be merged

Examples


dbc <- nodbi::src_sqlite(
    dbname = system.file("extdata", "demo.sqlite", package = "ctrdata"),
    collection = "my_trials"
)

df <- dbGetFieldsIntoDf(
    fields = c("overall_status", "x5_trial_status"),
    con = dbc
)

statusvalues <- list(
    "ongoing" = c("Recruiting", "Active", "Ongoing"),
    "completed" = c("Completed", "Prematurely Ended", "Terminated"),
    "other" = c("Withdrawn", "Suspended", "No longer available")
)

dfMergeVariablesRelevel(
    df = df,
    colnames = 'contains("status")',
    levelslist = statusvalues
)
#> Columns identified to be merged: overall_status, x5_trial_status
#>  [1] completed completed completed completed completed completed completed
#>  [8] completed completed completed completed completed completed completed
#> [15] <NA>      <NA>      completed <NA>      completed
#> Levels: ongoing completed other