Merge variables, keeping type, and optionally relevel factors
Source:R/dfMergeVariablesRelevel.R
dfMergeVariablesRelevel.Rd
Merge variables in a data frame such as returned by dbGetFieldsIntoDf into a new variable, and optionally also map its values to new levels.
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).
Examples
dbc <- nodbi::src_sqlite(
dbname = system.file("extdata", "demo.sqlite", package = "ctrdata"),
collection = "my_trials",
RSQLite::SQLITE_RO)
#> RSQLite version has enabled accelerating docdb_create() and docdb_update() functions when used with value = <NDJSON file name>.
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 ongoing completed completed completed completed
#> [8] <NA> <NA> completed <NA> completed
#> Levels: ongoing completed other