Skip to contents

This function checks for mismatches between a specified column in two dataframes using the setdiff() function. It provides detailed feedback on whether the column values match between the two dataframes, and highlights any discrepancies.

Usage

verify_col_match(data1, data2, col)

Arguments

data1

The first dataframe to compare.

data2

The second dataframe to compare.

col

A string specifying the name of the column to compare. This column must exist in both dataframes.

Value

The function does not return any value, but prints messages about column matching and discrepancies to the console.

Details

The function compares the values in a specified column from two dataframes:

  1. If all values match, it prints a confirmation message that the dataframes are ready for further data wrangling.

  2. If there are mismatches, the function checks the number of mismatched values in each dataframe and provides details on where the discrepancies lie.

  3. If both dataframes have the same number of mismatches, it suggests the possibility of typos.

Author

Zachary Amir & ChatGPT

Examples

# Example usage:
df1 <- data.frame(ID = c(1, 2, 3, 4), Name = c("Alice", "Bob", "Charlie", "David"))
df2 <- data.frame(ID = c(1, 2, 4, 5), Name = c("Alice", "Bob", "Charlie", "Eve"))

# Verify column match for 'ID'
verify_col_match(df1, df2, col = "ID")
#> The number of mis-matched values in the first and second dataframes are the same. Take special attention when ensuring a match, these could be the result of typos!
#> 
#>  The mis-matched values from the first dataframe are: 3 
#> 
#>  The mis-matched values from the second dataframe are: 5

# Verify column match for 'Name'
verify_col_match(df1, df2, col = "Name")
#> The number of mis-matched values in the first and second dataframes are the same. Take special attention when ensuring a match, these could be the result of typos!
#> 
#>  The mis-matched values from the first dataframe are: David 
#> 
#>  The mis-matched values from the second dataframe are: Eve