Chapter 3
R
1
Chapter 3
R Objects
3.1
Introduction of R Objects
The entities R operates on are technically known as objects. Examples are vectors of numeric
(real) or complex values, vectors of logical values and vectors of character strings. These are
known as “atomic” structures since their components are all of the same type, or mode, namely
numeric, complex, logical, character and raw.
Use the
is()
command to determine what an
object is.
1.
Mode of Objects
Vectors must have their values all of the same mode. Thus any given vector must be
unambiguously either logical, numeric, complex, character or raw.
a.
Numeric
is the default value type for most numbers. An integer is a subset of the
numeric class, and may be used as a numeric value. You can perform any type of math or
logical operation on numeric values, including:
> log(3 * 4 * (2 + pi))
# Note that pi is a built-in constant,
[1] 4.122270
# and log() the natural log function.
> 2 > 3
# Basic logical operations, including >,
[1] FALSE
# <, >= (greater than or equals),
# <= (less than or equals), == (exactly
# equals), and != (not equals).
> 3 >= 2 && 100 == 1000/10 # Advanced logical operations, including
[1] TRUE
# & (and), && (if and only if), | (or),
# and || (either or).
Note that
Inf
(infinity),
-Inf
(negative infinity),
NA
(missing value), and
NaN
(not a
number) are special numeric values on which most math operations will fail. (Logical
operations will work, however.)