Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

R Logical Operators


Share

Logical Operators

As with comparison operators, the result of a logical operator is TRUE or FALSE.

Logical operators are used to combine conditions. For example, to check if a number is between 3 and 10, it must be greater than 3 and less than 10:

Example

x <- 5

x > 3 & x < 10
Try it Yourself »

A list of the logical operators:

Operator Name Example Description Try it
& AND x > 3 & x < 10 Returns TRUE if both conditions are TRUE Try it »
| OR x > 3 | x > 10 Returns TRUE if at least one of the conditions is TRUE Try it »
! NOT !(x > 3 & x < 10) Reverses the result: TRUE becomes FALSE, and FALSE becomes TRUE Try it »

This table shows the result of & and | for all combinations of two values:

a b a & b a | b
TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE
FALSE TRUE FALSE TRUE
FALSE FALSE FALSE FALSE

&& and ||

R also has && and ||. They work like & and |, but only with one value on each side. They are mostly used in if statements, which you will learn about in the If...Else chapter:

Example

x <- 5

x > 3 && x < 10
x > 10 || x == 5
Try it Yourself »

& and | can compare many values at once. Here, R compares the first values, then the second values, and then the third values:

Example

c(TRUE, FALSE, TRUE) & c(TRUE, TRUE, FALSE)
Try it Yourself »

You will learn more about c() in the Vectors chapter.



Real-Life Example: Login Check

Logical operators are often used to check access rights, for example on a website:

Example

is_logged_in <- TRUE
is_admin <- FALSE

is_logged_in & !is_admin
is_logged_in | is_admin
!is_logged_in
Try it Yourself »
  • is_logged_in & !is_admin is TRUE: the user is logged in, and is not an admin, so it is a regular user.
  • is_logged_in | is_admin is TRUE: at least one of them is TRUE, so the user has access.
  • !is_logged_in is FALSE: the user is logged in.


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.