Let’s start by looking at constants. Constants are the basic building blocks for data objects in R: numbers, character values, and symbols.
Numbers are interpreted literally in R:
> 1.1 [1] 1.1 > 2 [1] 2 > 2^1023 [1] 8.988466e+307
You may specify values in hexadecimal notation by prefixing them
with 0x
:
> 0x1 [1] 1 > 0xFFFF [1] 65535
By default, numbers in R expressions are interpreted as double-precision floating-point numbers, even when you enter simple integers:
> typeof(1)
[1] "double"
If you want an integer, you can use the sequence notation or the
as
function to obtain an
integer:
> typeof(1:1) [1] "integer" > typeof(as(1, "integer")) [1] "integer"
The sequence operator
will return a vector of integers between a
:b
and a
. To combine an
arbitrary set of numbers into a vector, use the b
c
function:
> v <- c(173, 12, 1.12312, -93)
R allows a lot of flexibility when entering numbers. However, there is a limit to the size and precision of numbers that R can represent:
> # limits of precision > (2^1023 + 1) == 2^1023 [1] TRUE > # limits of size > 2^1024 [1] Inf
In practice, this is rarely a problem. Most R users will load data from other sources on a computer (like a database) that also can’t represent very large numbers.
R also supports complex numbers. Complex values are written as real_part
+
.
For example:imaginary_part
i
> 0+1i ^ 2 [1] -1+0i > sqrt(-1+0i) [1] 0+1i > exp(0+1i * pi) [1] -1+0i
Note that the sqrt
function
returns a value of the same type as its input; it will
return the value 0+1i
when passed
-1+0i
but will return an NaN
value when just passed the numeric value
-1
:
> sqrt(-1)
[1] NaN
Warning message:
In sqrt(-1) : NaNs produced
A character object contains all the text between a pair of quotes. Most commonly, character objects are denoted by double quotes:
> "hello"
[1] "hello"
A character string may also be enclosed by single quotes:
> 'hello'
[1] "hello"
This can be convenient if the enclosed text contains double quotes (or vice versa). Equivalently, you may also escape the quotes by placing a backslash in front of each quote:
> identical("\"hello\"", '"hello"') [1] TRUE > identical('\'hello\'', "'hello'") [1] TRUE
These examples are all vectors with only one element. To stitch
together longer vectors, use the c
function:
> numbers <- c("one", "two", "three", "four", "five") > numbers [1] "one" "two" "three" "four" "five"
An important class of constants is symbols. A symbol is an
object in R that refers to another object; a symbol is the name of a
variable in R. For example, let’s assign the numeric value 1
to the symbol x
:
> x <- 1
In this expression, x
is a
symbol. The statement x <- 1
means
“map the symbol x
to the numeric
value 1
in the current environment.”
(We’ll discuss environments in Chapter 8.)
A symbol that begins with a character and contains other characters, numbers, periods, and underscores may be used directly in R statements. Here are a few examples of symbol names that can be typed without escape characters:
> x <- 1 > # case matters > x1 <- 1 > X1 <- 2 > x1 [1] 1 > X1 [1] 2 > x1.1 <- 1 > x1.1_1 <- 1
Some symbols contain special syntax. In order to refer to
these objects, you enclose them in backquotes. For example, to get help
on the assignment operator (<-
),
you would use a command like this:
?`<-`
If you really wanted to, you could use backquotes to define a symbol that contains special characters or starts with a number:
> `1+2=3` <- "hello" > `1+2=3` [1] "hello"
Not all words are valid as symbols; some words are reserved in R.
Specifically, you can’t use if
,
else
, repeat
, while
, function
, for
, in
,
next
, break
, TRUE
, FALSE
, NULL
, Inf
,
NaN
, NA
, NA_integer_
, NA_real_
, NA_complex_
, NA_character_
, ...
, ..1
,
..2
, ..3
, ..4
,
..5
, ..6
, ..7
,
..8
, or ..9
.
You can redefine primitive functions that are not on this list.
For example, when you start R, the symbol c
normally refers to the primitive function
c
, which combines elements into
vectors:
> c
function (..., recursive = FALSE) .Primitive("c")
However, you can redefine the symbol c
to point to something else:
> c <- 1 > c [1] 1
Even after you redefine the symbol c
, you can continue to use the “combine”
function as before:
> v <- c(1, 2, 3) > v [1] 1 2 3
See Chapter 2 for more information on the combine function.