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. See ctrdata-trial-concepts for pre-defined cross-register concepts that are already implemented based on merging fields from different registers and calculating a new field.
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",
flags = RSQLite::SQLITE_RO)
df <- dbGetFieldsIntoDf(
fields = c(
"protocolSection.eligibilityModule.healthyVolunteers",
"f31_healthy_volunteers",
"eligibility.healthy_volunteers"
),
con = dbc
)
#> Querying database (3 fields)...
table(
dfMergeVariablesRelevel(
df = df,
colnames = 'matches("healthy")'
))
#> Columns identified to be merged: protocolSection.eligibilityModule.healthyVolunteers, f31_healthy_volunteers, eligibility.healthy_volunteers
#>
#> FALSE
#> 16