# str() inspects the internal structure of an object
str(TRUE) logi TRUE
Aina sa vola?
The chapter (Chapter 31) provide the syntax to perform arithmetic operation Chapter 1. For instance the multiplication is encoded as asterisk *. Notable symbols that have not yet been used include inequality operators (<, >, >=, <=) and the equality operator (=).
What does it mean to write 2 < 3, or 2 > 3? The first expression is a correct statement (true), while the other is incorrect (false). This concept applies directly to programming: these symbols serve as comparisons between the left-hand and right-hand side. A Boolean variable is a specialized object type that stores the result of such comparisons as either true or false.
In data-science, Boolean objects can encode qualitative variables (Chapter 18) when there are two outcomes: dead or alive, present or absent, or true or false. While strings or numeric types can also store binary data, Booleans are more explicit because they are restricted to exactly two values.
A Boolean variable type represents binary truth values used to evaluate logical conditions and control program flow.
To make it explicit, Booleans are specifically represented by TRUE or FALSE in R (T and F are also shorthand alternatives).
Syntax for relational operations:
a is strictly smaller to b, the expression is a < b.a is smaller than or equal to b the expression is a <= b;a is strictly larger to b the expression: a > b.a is larger than or equal to b the expression is a >= b;Syntax for testing equality: because = is used for variable assignment and in function arguments (Chapter 40), the convention is to use double equal signs ==. Thus, the expression to evalute whether a is equal to b is written as a == b^.
Syntax for testing inequality: to evaluate whether a is different from b, the expression is written as a != b.
In R, the boolean type is encoded as logi, short for logical
# str() inspects the internal structure of an object
str(TRUE) logi TRUE
# Define variables
var1 <- 3^2
var2 <- 2^3
# Test for equality (returns FALSE)
var1 == var2[1] FALSE
# Test for inequality (returns TRUE)
var1 != var2[1] TRUE
# Evaluate if var1 is strictly smaller than var2 (returns FALSE)
var1 < var2[1] FALSE
# Evaluate if var1 is greater than or equal to 1 (returns TRUE)
var1 >= 1[1] TRUE
These symbols have the same meaning (as those in arithmetic operatio Chapter 31) are the same in most programming languages.
While it is convenient to encode data as dead or alive or other symbol (maty and velona) during data entry (Chapter 38). Converting them to boolean TRUE and FALSE is generally done during the data cleaning phase.
It is better to use TRUE and FALSE when encoding boolean variable. While T and F are shorthand, they could be mixed with variable name. In contrast, the syntax TRUE and FALSE are protected and thus cannot be used otherwise.
NA is a third special protect to mean empty.
In R, it is possible to perform arithmetic operation on boolean variable. The operation below works because R coerce them as 0 and 1 and thus proceed forward with the calculation.
# Assign Boolean values (FALSE implicitly equals 0, TRUE implicitly equals 1)
a <- FALSE
b <- TRUE
# Arithmetic operations automatically coerce Booleans to numbers (0 + 1 = 1)
a + b[1] 1
# Multiplication with a zero value yields zero (0 * 1 = 0)
a * b[1] 0
# Subtraction of numeric representations (0 - 1 = -1)
a - b[1] -1
# Division of zero by one (0 / 1 = 0)
a / b[1] 0
Boolean originates from mathematical logic (Boolean algebra). Binary refers specifically to systems with two possible states, while logical indicates values evaluated as true or false.