R Logical Operators to Know for Intro to Programming in R

Logical operators in R are essential for making comparisons and controlling the flow of your code. They help you evaluate conditions, filter data, and create complex statements, making them a key part of programming in R.

  1. == (Equal to)

    • Compares two values to check if they are the same.
    • Returns TRUE if the values are equal, otherwise FALSE.
    • Can be used with numbers, characters, and logical values.
  2. != (Not equal to)

    • Checks if two values are different from each other.
    • Returns TRUE if the values are not equal, otherwise FALSE.
    • Useful for filtering data or conditions where inequality is required.
  3. (Greater than)

    • Compares two values to determine if the first is larger than the second.
    • Returns TRUE if the first value is greater, otherwise FALSE.
    • Commonly used in conditional statements and loops.
  4. < (Less than)

    • Compares two values to see if the first is smaller than the second.
    • Returns TRUE if the first value is less, otherwise FALSE.
    • Often used in data analysis to filter or categorize data.
  5. = (Greater than or equal to)

    • Checks if the first value is either greater than or equal to the second.
    • Returns TRUE if the condition is met, otherwise FALSE.
    • Useful for inclusive comparisons in statistical analysis.
  6. <= (Less than or equal to)

    • Determines if the first value is either less than or equal to the second.
    • Returns TRUE if the condition holds, otherwise FALSE.
    • Frequently used in data validation and conditional checks.
  7. & (Logical AND)

    • Combines two logical conditions and returns TRUE only if both are TRUE.
    • Useful for creating complex conditions in if statements.
    • Evaluates both conditions even if the first is FALSE.
  8. | (Logical OR)

    • Combines two logical conditions and returns TRUE if at least one is TRUE.
    • Allows for flexibility in conditional statements.
    • Evaluates both conditions even if the first is TRUE.
  9. ! (Logical NOT)

    • Reverses the logical value of a condition.
    • Returns TRUE if the condition is FALSE, and vice versa.
    • Useful for negating conditions in control flow.
  10. %in% (Value matching)

    • Checks if a value exists within a specified vector or list.
    • Returns TRUE if the value is found, otherwise FALSE.
    • Ideal for filtering data based on membership in a set.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.