Post

DP06 Change Values based on External Table

Description

There are situations when certain fields require updates using information gathered from another table. Similar to Excel’s Match & Index feature, data is extracted from another table and applied to specific records through a common key shared by both tables. In this example, we update the names of practices that have outdated entries from the source. These new names are stored in a dedicated table, which is created within the script (hardcoded). This process is commonly referred to as value imputation. Within this script, a specific function has been coded utilizing the ‘match’ function from the base package.

R Script - Change Values based on External Table

Table with the Names to Change

OriginalNameChangedName
CAPE FEAR FAMILY MEDICAL CARE 2344 WALTER REEDCAPE FEAR FAMILY MED CARE
CAPE FEAR FAMILY MEDICAL CARE 543 OWEN DRCAPE FEAR FAMILY MED CARE
CAPE FEAR FAMILY MEDICAL CARE 465 OWEN DRCAPE FEAR FAMILY MED CARE
CAROLINA RHEUMATOLOGY AND INTERNAL MEDICINECAPE FEAR VALLEY PRIMARY CARE
FAYETTEVILLE GERIATRIC & INTERNAL MEDICINECAROLINA PRIMARY & INTERNAL MEDICINE
WADE FAMILY MEDICAL CENTERWADE HEALTH SERVICES

Detail of info Previous to Imputation.

06 Pts per Practice before Inputation Result: Comparison of Differences

Function for Imputation.

For each value in the OriginalName, the function iterates through the DataFrame to find matching values and replaces each of these values with its corresponding ChangedName value.

1
2
3
4
5
6
7
8
9
10
fx_change_Practice_Names <- function(PCP_field_name, df, dfChanges){
  LocPCP <- match(PCP_field_name,names(df))
  for(i in 1:nrow(dfChanges)){
    print(paste0("Records with ",dfChanges[i,1],": ", df %>% 
                   filter(df[ ,LocPCP] == dfChanges[i,1]) %>% tally()))
    df[ ,LocPCP][df[ ,LocPCP] == dfChanges[i,1]] <- dfChanges[i,2]
    print(paste0("  -------->   CHANGED TO :", dfChanges[i,2]))
  }
  df
}

Function executed. Change Practice names showing number of changed records.

06 Function Executed Result: Function applied

Final Results. Practice with changed names and their records.

06 Results Result: Results After imputation

__

End of Post

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