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:
Syntax
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
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
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
- Writing
trueorTrueinstead ofTRUE, which R does not recognise. - Using
TandFas if they were reserved, when they are ordinary variables that can be overwritten. - Using
=instead of==when comparing, which assigns or gives an error instead of aTRUE/FALSE.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: