Post

DP18 Merge multiple fields of Labels

Description

The script takes each record representing a patient, scan across a group of fields, in this case medical conditions, and creates a compounded field with the names of the conditions that the patient has. This process is required for reporting purposes.

In order to create the compounded field the script merges the names using apply-collapse. Finally, all the resulting consecutive commas are removed with a regular expression.

R Script - Merge multiple fields of labels

Initial Data: Medical Conditions for 11 patients (sample)

rowAsthmaCVA.StrokeChr.Kidney.DisChronic.PainCOPDDiabetesCHF
192YesYesNoYesNoNoYes
111NoYesYesNoNoYesNo
128NoNoNoYesYesYesYes
163NoNoYesYesNoYesNo
87NoYesNoYesNoNoNo
74YesNoNoNoNoNoNo
37YesYesNoNoYesNoNo
7NoYesNoNoYesYesNo
396NoNoNoYesYesYesYes
311NoNoNoYesNoYesYes
306NoYesNoNoNoNoNo

18 Initial Data Some Medical Conditions Initial Data in R console

Transforming “Yes” labels using a function

The next function converts the values “Yes” into the name of the condition, and assign blank for the “No” values.

1
2
3
4
5
6
7
# Defined function to convert the "Yes" values into the name of the condition
fxonlyfieldnamestochangevalues <- function(df){
  for(ii in seq_along(df)){
    df[,ii] <- ifelse(df[,ii]  == "Yes", names(df)[ii],"")
  }
  df
}

Applying the function

1
dataselected <- fxonlyfieldnamestochangevalues(dataselected)

Partial Results

18 Partial Results Partial Results

Merge the values

Merge the values into a compounded field and remove the unnecessary commas.

1
2
3
4
5
6
# Code to merge the labels and remove commas
allconditions <- apply( dfclinicalconditions,1,paste,collapse = ",")
allconditions <- gsub("^,*|(?<=,),|,*$","", allconditions, perl=T)
allconditions <- gsub(",", ", ", allconditions)
# Add to the data frame as one column
dfclinicalconditions$onecolumn <- allconditions

Final Result

The conditions were merged into one field.

18 Final Result Some Medical Conditions Merged into one Field. Final Result

__

End of Post

This post is licensed under CC BY 4.0 by the author.