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.
Link to the Complete Script in Github
R Script - Merge multiple fields of labels
Initial Data: Medical Conditions for 11 patients (sample)
row | Asthma | CVA.Stroke | Chr.Kidney.Dis | Chronic.Pain | COPD | Diabetes | CHF |
---|---|---|---|---|---|---|---|
192 | Yes | Yes | No | Yes | No | No | Yes |
111 | No | Yes | Yes | No | No | Yes | No |
128 | No | No | No | Yes | Yes | Yes | Yes |
163 | No | No | Yes | Yes | No | Yes | No |
87 | No | Yes | No | Yes | No | No | No |
74 | Yes | No | No | No | No | No | No |
37 | Yes | Yes | No | No | Yes | No | No |
7 | No | Yes | No | No | Yes | Yes | No |
396 | No | No | No | Yes | Yes | Yes | Yes |
311 | No | No | No | Yes | No | Yes | Yes |
306 | No | Yes | No | No | No | No | No |
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
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.
Some Medical Conditions Merged into one Field. Final Result
__
End of Post