Learn R Programming

ctrdata (version 1.15.2)

dfMergeVariablesRelevel: Merge variables, keeping type, and optionally relevel factors

Description

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)

Value

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

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

Run this code

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)

Run the code above in your browser using DataLab