In this lesson the student will learn to declare variables, the use of simple expressions and compound expressions.
Variable names must start with a letter, which can be followed by
letters, numbers, or the symbols period .
and
underline _
.
Uppercase and lowercase letters are handled as separate characters.
To assign a value to a variable, use the assignment symbol
<-
.
Unlike other programming languages, in R it is not necessary to declare the name and type of the variables.
To create a variable called \(b\) that takes the value \(3.9\), it is enough to write:
b <- 3.9
b
## [1] 3.9
Now, the assignment sign <-
should be interpreted as
a left-pointing arrow indicating that the value 3.9
will be
put into the variable b
(the =
also works for
an assignment of right to left). It can also be assigned in the opposite
direction by using ->
.
3.9 -> b
b
## [1] 3.9
To know the content of a variable, it is enough to execute a line of
code where the name of the variable is. It is also possible to do this
with the use of the print(a)
and cat(a)
functions.
b <- 3.9
b
## [1] 3.9
print(b)
## [1] 3.9
cat(b)
## 3.9
If you want a variable to store a string/sequence of characters (a string), the characters must be enclosed in double quotes (or single quotes).
persona <- "Juan"
persona
## [1] "Juan"
In the same way, a variable can store the boolean values
trueTRUE
and false FALSE
.
flag <- TRUE
flag
## [1] TRUE
Some variable names are already associated with some object, for example,
pi
## [1] 3.141593
Legacy functions are a special type of variable, so their names
should not be used to name their own variables or functions. For
example, this is the case of the word rnorm
.
print(rnorm)
## function (n, mean = 0, sd = 1)
## .Call(C_rnorm, n, mean, sd)
## <bytecode: 0x00000253a21b26e8>
## <environment: namespace:stats>
NULL
means or represents that a variable is “empty”.
p <- NULL
cat("`p`:",p,
"\n`is.null(p)`:",is.null(p))
## `p`:
## `is.null(p)`: TRUE
It is important that you know the following expressions:
Inf
representa al “número” infinitoNA
significa Not AvailableNaN
significa Not a NumberThe is.infinite()
function returns a boolean value
indicating whether a variable corresponds to Inf
.
p <- (200-195) / 0
cat("`p`:",p,
"\n`is.infinite(p)`:",is.infinite(p))
## `p`: Inf
## `is.infinite(p)`: TRUE
p <- NA
cat("`p`:",p,
"\n`is.na(p)`:",is.na(p))
## `p`: NA
## `is.na(p)`: TRUE
p <- 0 / 0
cat("`p`:",p,
"\n`is.nan(p)`:",is.nan(p))
## `p`: NaN
## `is.nan(p)`: TRUE
Unlike other programming languages, in R the use of the symbol “;” It is only necessary if you want to execute more than one statement on the same line of code.
p <- (200-195) / 0
p; is.na(p); is.infinite(p); is.nan(p)
## [1] Inf
## [1] FALSE
## [1] TRUE
## [1] FALSE
In R, multiple statements or expressions can be grouped together by using square brackets. The result of a compound statement is the result of evaluating the last expression inside the brackets. A typical use of compound expressions is to provide the parameters for a function, for example
{p <- 20; c <- 9; p/c}
## [1] 2.222222
It can be seen that this expression is made up of three simple instructions, in the first one the value \(20\) is assigned to the variable \(p\), in the second one the value \(9\) is assigned to the variable \(c\), and finally the value is requested by dividing the variable \(p\) by the variable \(c\).