#
# CHAPTER 12: Logical elements and operations
#
# elements of objects have "attributes"
# two important attributes are the elements "mode" and "length
# common modes: "null", "logical", "integer", "numeric",
# "complex", "character", "list"
# there are other possible attributes (names, dimnames, class)
#
x = TRUE # logical
x
TRUE = 17 # set TRUE to 17
TRUE # TRUE remains TRUE
T = 17 # set T to 17
T # display T
rm(T) # remove the object T
x = c(T, F, T, F, T, F) # x is a vector of logical elements
x # display x
mode(x) # the elements of x are logical
length(x) # x has six elements
any(x) # are there any TRUE elements in x?
all(x) # are all the elements in x TRUE?
!x # not x
y = c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE) # y is a vector of logicals
y # display y
x & y # x and y (vectorized and operator)
x | y # x or y (vectorized or operator)
x && y # x and y (non-vectorized and operator)
x || y # x or y (non-vectorized or operator)
x | (y - 1) # nonzero TRUE, zero FALSE (automatic coercion)
sum(x) # FALSE is 0; TRUE is 1 (automatic coercion)
mean(x) # sample mean (fraction of TRUEs)
range(x) # sample range
diff(x) # differences
z = 1:6 # first six integers
z[x] # logical vector used for subscripting
z[x] = 8 # set z elements associated with TRUE in x to 8
z # display z
z[y] = z[y] - 1 # subtract 1 from the z elements with TRUE in y
z # display z
class(NULL) # NULL
class(TRUE) # logical element
class(3L) # integer element
class(3) # numeric element
class(3 + 4i) # complex element
class("yak") # character element