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

Logical Type

In this page:

  1. Logical Type

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")
🔒

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.