18 Expressions
In this chapter you will learn about R expressions which is a technical concept that appears everywhere in all R programming structures (e.g. functions, conditionals, loops). This chapter, by the way, is the shortest of the book. But its implications are fundamental to get a solid understanding of programming structures in R.
18.1 R Expressions
Before moving on with more programming structures we must first talk about R expressions.
18.1.1 Simple Expressions
So far you’ve been writing several lines of code in R, most of which have been simple expressions such as:
= 1000
deposit = 0.02
rate = 3 year
The expression deposit = 1000
is an assignment statement because we assign
the number 1000
to the name deposit
. It is also a simple expression.
The same can be said about the expressions for rate
and year
.
Simple expressions are fairly common but they are not the only ones. It turns out that there is another class of expressions known as compound expressions.
18.1.2 Compound Expressions
R programs are made up of expressions which can be either simple expressions or compound expressions. Compound expressions consist of simple expressions separated by semicolons or newlines, and grouped within braces.
# structure of a compound expression
# with simple expressions separated by semicolons
{expression_1; expression_2; ...; expression_n}
# structure of a compound expression
# with simple expressions separated by newlines
{
expression_1
expression_2
expression_n }
Here’s a less abstract example:
# simple expressions separated by semicolons
"first"; 1; 2; 3; "last"}
{> [1] "last"
# simple expressions separated by newlines
{"first"
1
2
3
"last"
}> [1] "last"
Writing compound expressions like those in the previous example is not something common among R users. Although the expressions are perfectly valid, these examples are very dummy (just for illustration purposes). By the way, I strongly discourage you from grouping multiple expressions with semicolons because it makes it difficult to inspect things.
What does R do with a compound expression? When R encounters a compound expression, it handles everything inside of it as a single unit or a single block of code.
What is the purpose of a compound expression? This kind of expression plays an important role but it is typically used together with other programming structures (e.g. functions, conditionals, loops).
18.1.3 Every expression has a value
A fundamental notion about expressions is that every expression in R has a value.
Consider this simple expression:
<- 5 a
If I ask you: What is the value of a
?, you should have no trouble answering
this question. You know that a
has the value 5.
What about this other simple expression:
<- 1:5 b
What is the value of b
? You know as well that the value of b
is the
numeric sequence given by 1 2 3 4 5
.
Now, let’s consider the following compound expression:
<- {5; 10} x
Note that the entire expression is assigned to x
. Let me ask you the same
question. What is the value of x
? Is it:
- 5?
- 10?
- 5, 10?
Let’s find out the answer by taking a look at x
:
x> [1] 10
Mmmm, this is interesting. As you can tell, x
has a single value, and it’s
not 5
but 10
. Out of curiosity, let’s also consider this other compound
expression for y
and examine its value:
<- {
y 15
10
5
}
y> [1] 5
Same thing, y
has a single value, the number 5
, which happens to be the
last statement inside the expression that was evaluated. This is precisely
the essence of an R expression. Every R expression has a value, the value of
the last statement that gets evaluated.
To make sure you don’t forget it, repeat this mantra:
Every expression in R has a value: the value of the last evaluated statement.
Every expression in R has a value: the value of the last evaluated statement.
Every expression in R has a value: the value of the last evaluated statement.
18.1.4 Assignments within Compound Expressions
It is possible to have assignments within compound expressions. For instance:
# simple expressions (made up of assignments) separated by newlines
{<- 1
one <- pi
pie <- "z"
zee }
This compound expression contains three simple expressions, all of which are
assignments. Interestingly, when an R expressions contains such assignments, the
values of the variables can be used in later expressions. In other words, you
can refer later to one
or pie
or zee
:
# simple expressions (made up of assignments) separated by newlines
{<- 1
one <- pi
pie <- "z"
zee
}
one> [1] 1
pie> [1] 3.141593
zee> [1] "z"
Here’s another example:
<- { x = 10 ; y = x^2; x + y }
z
x> [1] 10
y> [1] 100
z> [1] 110
Now that we’ve introduced the concept of compound expressions, we can move on to next chapter where we introduce conditional structures.