← Back to R Course | Chapter 2: Variables & Types | Lesson 5 of 7

Logical Type

A logical value is just TRUE or FALSE, like a light switch that is on or off. R uses it to answer yes-or-no questions.

In this page:

  1. Logical Type
Syntax
r
flag <- TRUE
result <- value1 > value2    # logical result

Logical Type

The logical type holds TRUE or FALSE (or their shorthand T/F), and is the result of every comparison.

Logical values behave as 1 (TRUE) and 0 (FALSE) when used in arithmetic, which makes counting TRUE values with sum() a common idiom. Comparison operators like ==, <, > always produce logical results.

Note: sum(logical_vector) counts how many elements are TRUE, since TRUE is treated as 1 in arithmetic.

Example: Logical Type

markup
is_active <- TRUE
is_admin <- FALSE

cat(class(is_active), "\n")
cat(is_active + is_active, "\n")  # TRUE + TRUE = 2
cat(10 > 5, "\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing true or True instead of TRUE, which R does not recognise.
  2. Using T and F as if they were reserved, when they are ordinary variables that can be overwritten.
  3. Using = instead of == when comparing, which assigns or gives an error instead of a TRUE / FALSE.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.