Logical Type
In this page:
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
is_active <- TRUE
is_admin <- FALSE
cat(class(is_active), "\n")
cat(is_active + is_active, "\n") # TRUE + TRUE = 2
cat(10 > 5, "\n")
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: