2.4 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Compare Two Columns in R (With Examples)

How to Compare Two Columns in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories