We now know how to enter code for R to interpret, whether directly entering it into R's command line or sending it to the command line from a code editor. Our next step will be to see how to use the simplest operations: arithmetic and logical operators and functions.
The standard arithmetic operators in R are as follows:
+
: Addition-
: Subtraction*
: Multiplication/
: Division^
: PowerThe following examples demonstrate the usage of these operators:
> 5+3 [1] 8 > 4-5 [1] -1 > 1*10 [1] 10 > 1/10 [1] 0.1 > 2^3 [1] 8
Parentheses can be used to construct more elaborate expressions, as follows:
> 2*(3+1) [1] 8 > 5^(1+1) [1] 25
It is better to use parentheses even when it is not required to make the code clearer.
Another very useful symbol is #
. All code to the right of this symbol is not interpreted. Let's take a look at the following example:
> 1*2 # *3 [1] 2
The #
symbol is helpful for adding comments within the code to explain what each code segment does, for other people (or oneself, at a later time of reference) to understand it:
> 5+5 # Adding 5 and 5 [1] 10
Note that R ignores spaces between the components of an expression:
> 1+ 1 [1] 2
Conditions are expressions that have a yes/no answer (the statement can be either true or false). When interpreting a conditional expression, R returns a logical value, either TRUE
for a true expression or FALSE
for a false expression. A third option, NA
, which stands for Not Available, is used when there is not enough information to determine whether the expression is true or false (NA
values will be discussed in the next chapter).
The logical operators in R are summarized as follows:
For example, we can use condition operators to compare between two numbers as follows:
> 1<2 [1] TRUE > 1>2 [1] FALSE > 2>2 [1] FALSE > 2>=2 [1] TRUE > 2!=2 [1] FALSE
The and (&
) and or (|
) operators can be used to construct more complex expressions as follows:
> (1<10) & (10<100) [1] TRUE > (1<10) & (10>100) [1] FALSE > (1<10) | (10<100) [1] TRUE > (1<10) | (10>100) [1] TRUE
As you can see in the preceding examples, when the expressions at both the sides of the &
operator are true, TRUE
is returned; otherwise, FALSE
is returned (refer to the first two expressions). When at least one of the expressions at either side of the |
operator is true, TRUE
is returned; otherwise, FALSE
is returned (refer to the last two expressions).
Two other useful conditional operators (==
and !=
) are used for testing equality and inequality, respectively. These operators are opposites from one another since a pair of objects can be either equal or non-equal to each other.
> 1 == 1 [1] TRUE > 1 == 2 [1] FALSE > 1 != 1 [1] FALSE > 1 != 2 [1] TRUE
As you can see in the preceding examples, when using the ==
operator, TRUE
is returned if the compared objects are equal; otherwise FALSE
is returned (refer to expressions 1 and 2). With !=
it is the other way around (refer to expressions 3 and 4).
The last operator that we are going to cover is the not operator (!
). This operator reverses the resulting logical value, from TRUE
to FALSE
or from FALSE
to TRUE
. This is used in cases when it is more convenient to ask whether a condition is not satisfied. Let's take a look at the following example:
> 1 == 1 [1] TRUE > !(1 == 1) [1] FALSE > (1 == 1) & (2 == 2) [1] TRUE > (1 == 1) & !(2 == 2) [1] FALSE
In mathematics, a function is a relation between a set of inputs and a set of outputs with the property that each input is related to exactly one output. For example, the function y=2*x relates every input x with the output y, which is equal to x multiplied by 2. The function concept in R (and in programming in general) is very similar:
The most common (and the most useful) expressions in R are function calls. In fact, we have been using function calls all along, since the arithmetic operators are functions as well, which becomes apparent when using a different notation:
> 3*3 [1] 9 > "*"(3,3) [1] 9
A function is essentially a predefined set of instructions. There are plenty of built-in functions in R (functions that are automatically loaded into memory when starting R). Later, you will also learn how to use functions that are not automatically loaded, and how to define your own functions.
As you might have guessed from the previous example, a function call is composed of the function name, followed by the function's arguments within parentheses and separated by commas. For example, the function sqrt
returns the square root of its argument:
> sqrt(16) [1] 4
Error messages are printed when for some reason it is impossible to execute the expression that we have sent to the interpreter. For example, this can happen when one of the objects we refer to does not exist (refer to the preceding information box). Another example is trying to pass an inappropriate argument to a function. In R, character values are delimited by quotes. Trying to call a mathematical function on a character understandably produces an error:
> "oranges" + "apples" Error in "oranges" + "apples" : non-numeric argument to binary op$
Warning messages are returned when an expression can be interpreted but the system suspects that the respective employed method is inappropriate. For example, the square root of a negative number does not yield a number within the real number system. A Not a Number (NaN
) value is returned in such a case, along with a warning:
> sqrt(-2) [1] NaN Warning message: In sqrt(-2) : NaNs produced
R has a set of predefined symbols to represent special constant values, most of which we already mentioned:
A help page on every function in R can be reached by using the ?
operator (or the help
function). For example, the following expression opens the help page for the sqrt
function:
> ?sqrt
The same result is achieved by typing help(sqrt)
.
The structure of all help files on functions is similar, usually including a short description of what the function does, the list of its arguments, usage details, a description of the returned object, references, and examples. The help pages can seem intimidating at first, but with time they become clearer and more helpful for reminding oneself of the functions' usage details.
Another important source of information on R is the Internet. Entering a question or a task that we would like to perform (such as Googling r read raster file) into a web search engine usually yields a surprising amount of information from forums, blogs, and articles. Using these resources is inevitable when investigating new ways to utilize R.