4 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Use “OR” Operator in R (With Examples)

How to Use “OR” Operator 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...

You can use the | symbol as an “OR” operator in R.

For example, you can use the following basic syntax to filter for rows in a data frame in R that satisfy condition 1 or condition 2:

df[(condition1) | (condition2), ]

The following examples show how to use this “OR” operator in different scenarios.

Example 1: Use “OR” Operator to Filter Rows Based on Numeric Values in R

Suppose we have the following data frame in R:

#create data frame
df frame(team=c('A', 'A', 'B', 'B', 'B', 'B', 'C', 'C'),
                 points=c(25, 12, 15, 14, 19, 23, 25, 29),
                 assists=c(5, 7, 7, 9, 12, 9, 9, 4),
                 rebounds=c(11, 8, 10, 6, 6, 5, 9, 12))

#view data frame
df

  team points assists rebounds
1    A     25       5       11
2    A     12       7        8
3    B     15       7       10
4    B     14       9        6
5    B     19      12        6
6    B     23       9        5
7    C     25       9        9
8    C     29       4       12

We can use the following syntax to filter for rows in the data frame where the value in the points column is greater than 20 or the value in the assists column is equal to 9:

#filter rows where points > 20 or assists = 9
df[(df$points > 20) | (df$assists == 9), ]

  team points assists rebounds
1    A     25       5       11
4    B     14       9        6
6    B     23       9        5
7    C     25       9        9
8    C     29       4       12

The only rows returned are the ones where the points value is greater than 20 or the assists value is equal to 9.

Example 2: Use “OR” Operator to Filter Rows Based on String Values in R

Suppose we have the following data frame in R:

#create data frame
df frame(team=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 position=c('G', 'G', 'F', 'F', 'C', 'F', 'C', 'C'),
                 conference=c('W', 'W', 'W', 'W', 'E', 'E', 'E', 'E'),
                 points=c(11, 8, 10, 6, 6, 5, 9, 12))

#view data frame
df

  team position conference points
1    A        G          W     11
2    B        G          W      8
3    C        F          W     10
4    D        F          W      6
5    E        C          E      6
6    F        F          E      5
7    G        C          E      9
8    H        C          E     12

We can use the following syntax to filter for rows in the data frame where the value in the position column is equal to G or the value in the position column is equal to F or the value in the team column is equal to H:

#filter rows based on string values
df[(df$team == 'H') | (df$position == 'G') | (df$position == 'F'), ]

  team position conference points
1    A        G          W     11
2    B        G          W      8
3    C        F          W     10
4    D        F          W      6
6    F        F          E      5
8    H        C          E     12

The only rows returned are the ones that meet at least one of the three conditions that we specified.

Additional Resources

The following tutorials explain how to use other common operators in R:

How to Use Dollar Sign ($) Operator in R
How to Use the Tilde Operator (~) in R
How to Use “NOT IN” Operator 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