Home » How to Compare Two Columns in R (With Examples)

How to Compare Two Columns in R (With Examples)

by Erma Khan

Often you may want to compare two columns in R and write the results of the comparison to a third column.

You can easily do this by using the following syntax:

df$new_col ifelse(df$col1 > df$col2, 'A',
                ifelse(df$col1  df$col2, 'B', 'C'))

This single line of code does the following:

  • If column 1 is greater than column 2 then write ‘A’ as the output to the third column.
  • Otherwise, if column 1 is less than column 2 then write ‘B’ as the output..
  • Otherwise, write ‘C’ as the output.

The following example shows how to use this code in practice.

Example: Compare Two Columns in R

Suppose we have the following data frame that shows the number of goals scored by two soccer teams in five different matches:

#create data frame
df #view data frame
df

  A_points B_points
1        1        4
2        3        5
3        3        2
4        3        3
5        5        2

We can use the following code to compare the number of goals by row and output the winner of the match in a third column:

#compare A_points and B_points and output results to new column titled winner
df$winner ifelse(df$A_points > df$B_points, 'A',
               ifelse(df$A_points  df$B_points, 'B', 'Tie'))

#view data frame
df

  A_points B_points winner
1        1        4      B
2        3        5      B
3        3        2      A
4        3        3    Tie
5        5        2      A

The results of the comparison are shown in the new column called winner.

Additional Resources

How to Stack Data Frame Columns in R
How to Combine Two Columns into One in R
How to Loop Through Column Names in R

Related Posts