# Checking if Multiple Columns are Equal in R

**Steve's Data Tips and Tricks**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

# Introduction

When working with data in R, you might need to check if values across multiple columns are equal. This is a common task in data cleaning and preprocessing. In this blog, I’ll show you how to do this using base R, `dplyr`

, and `data.table`

. Let’s dive into some examples that demonstrate how to check if every column in a row is equal or if specific columns are equal.

# Examples

## Base R

Let’s start with a simple data frame:

df <- data.frame( A = c(1, 2, 3, 4), B = c(1, 2, 3, 5), C = c(1, 2, 3, 4) )

### Check if All Columns in a Row are Equal

To check if all columns in a row are equal, you can use the `apply`

function:

df$AllEqual <- apply(df, 1, function(row) all(row == row[1])) print(df)

A B C AllEqual 1 1 1 1 TRUE 2 2 2 2 TRUE 3 3 3 3 TRUE 4 4 5 4 FALSE

Here’s what the code does: - `apply(df, 1, ...)`

applies a function to each row of the data frame. - `function(row) all(row == row[1])`

checks if all elements in the row are equal to the first element of the row.

### Check if Specific Columns are Equal

To check if specific columns are equal, you can do something similar:

df$ABEqual <- df$A == df$B print(df)

A B C AllEqual ABEqual 1 1 1 1 TRUE TRUE 2 2 2 2 TRUE TRUE 3 3 3 3 TRUE TRUE 4 4 5 4 FALSE FALSE

This code creates a new column `ABEqual`

that is `TRUE`

if columns `A`

and `B`

are equal, and `FALSE`

otherwise.

## Using `dplyr`

Now let’s see how to do the same tasks using `dplyr`

, a popular package for data manipulation.

First, install and load the package if you haven’t already:

#install.packages("dplyr") library(dplyr)

### Check if All Columns in a Row are Equal

df <- df %>% rowwise() %>% mutate(AllEqual = all( c_across( everything()) == first(c_across(everything())) ) ) print(df)

# A tibble: 4 × 5 # Rowwise: A B C AllEqual ABEqual <dbl> <dbl> <dbl> <lgl> <lgl> 1 1 1 1 TRUE TRUE 2 2 2 2 FALSE TRUE 3 3 3 3 FALSE TRUE 4 4 5 4 FALSE FALSE

Here’s a breakdown: - `rowwise()`

groups the data frame by rows, allowing row-wise operations. - `mutate(AllEqual = all(c_across(everything()) == first(c_across(everything()))))`

creates a new column `AllEqual`

that checks if all values in the row are the same.

### Check if Specific Columns are Equal

df <- df %>% mutate(ABEqual = A == B) print(df)

# A tibble: 4 × 5 # Rowwise: A B C AllEqual ABEqual <dbl> <dbl> <dbl> <lgl> <lgl> 1 1 1 1 TRUE TRUE 2 2 2 2 FALSE TRUE 3 3 3 3 FALSE TRUE 4 4 5 4 FALSE FALSE

This code creates a new column `ABEqual`

in the same way as in base R.

## Using `data.table`

Finally, let’s use `data.table`

, another powerful package for data manipulation. Install and load the package if needed:

#install.packages("data.table") library(data.table)

Convert the data frame to a data table:

dt <- as.data.table(df)

### Check if All Columns in a Row are Equal

dt[, AllEqual := apply(.SD, 1, function(row) all(row == row[1]))] print(dt)

A B C AllEqual ABEqual <num> <num> <num> <lgcl> <lgcl> 1: 1 1 1 TRUE TRUE 2: 2 2 2 FALSE TRUE 3: 3 3 3 FALSE TRUE 4: 4 5 4 FALSE FALSE

`.SD`

refers to the subset of the data table.`apply(.SD, 1, function(row) all(row == row[1]))`

applies the function row-wise to check equality.

### Check if Specific Columns are Equal

dt[, ABEqual := A == B] print(dt)

A B C AllEqual ABEqual <num> <num> <num> <lgcl> <lgcl> 1: 1 1 1 TRUE TRUE 2: 2 2 2 FALSE TRUE 3: 3 3 3 FALSE TRUE 4: 4 5 4 FALSE FALSE

This creates a new column `ABEqual`

just like in the previous examples.

# Conclusion

Checking if multiple columns are equal is straightforward in R, whether you use base R, `dplyr`

, or `data.table`

. Each method has its advantages, and you can choose based on your preference or the specific needs of your project. I encourage you to try these examples on your own data and see how they work. Experimenting with different datasets can help you become more comfortable with these techniques.

Happy coding!

**leave a comment**for the author, please follow the link and comment on their blog:

**Steve's Data Tips and Tricks**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.