DP09 Apply changes to Filtered Group of Records
Description
When dealing with situations where a common transformation needs to be applied to multiple fields, the following script applies a method, often referred to as Batch Transformation.
In this scenario, the script focuses on fields related to medical conditions within the dataset. It addresses the issue of empty “” or N/A values by replacing them with the value “No.” This alteration serves the purpose of using “Yes” or “No” values for subsequent visualizations in a PowerBI chart. The transformations are executed using Dplyr functions mutate and Case_When.
Link to the Complete Script in Github
R Script - Apply changes to Medical Condition fields
Initial Data Format for the group of Fields
Identify the specific location within the dataset where the transformation needs to be performed. This applies specifically to the Medical Conditions fields.
Original Structure of values for Medical Conditions
1
2
3
# Replace empty value "" or NA by "No"
PPL_df[,RangeConditions][PPL_df[,RangeConditions]==""] <- "No"
PPL_df[,RangeConditions][is.na(PPL_df[,RangeConditions])] <- "No"
Another approach for multiple replacements with mutate and case_when
Example for only “Dual” field
1
2
PPL_df %>% mutate(Dual = case_when(Dual == "" ~ "No",
Dual == "YES" ~ "Yes"))
Results
Final arranged values for Medical Conditions
__
End of Post