Data Classes
Everything that R creates is an object. Each R object can have various attributes such as class, length, names, dimensions and metadata. You can obtain the attributes of an object with the attributes()
function.
There are three basic classes of data objects: numeric, character, and logical.
Numeric variables
The first command in the code block creates a variable called n
and sets its value to the numeric value 48. The <-
is an assignment operator in R. You can also use =
as an assignment operator as shown in the next example. The second command (on the second line) asks R to display the value of n
. The third line is output, where the value is displayed.
is a special number which denotes "infinity" (1 / 0 = Inf), which you should envisage as a statement with a valid limit.
(not a number) is another special number that indicates an undefined value, usually because of a disallowed mathematical operation. Taking the square root of a negative number would result in a
.
Note that nearly all mathematical operations, including addition, will accept positive or negative , or
, as inputs. You can check whether a variable is infinite, finite or
using the functions
,
and
, which return logical values (if run on vectors, as described in the later page on data objects, they return a vector of the same length of logical values, indicating whether each value is finite, infinite or
).
Character strings
Logical variables
Missing data is indicated by NA (not available), and is different than 0 for numeric data, "" for character data, or FALSE for logical data.
Additionally, complex numbers are available in the "base" R package; for example, you can evaluate the square root of -1 if you first cast it to complex using :
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)