2 + 1
[1] 3
3 - 1
[1] 2
4 * 2
[1] 8
15 / 5
[1] 3
3 ^ 2
[1] 9
3 ** 2
[1] 9
This chapter introduces some of the common operations you can do with R. To achieve this, we will use R as a simple calculator using its operators and getting a view of how its statements are structured, i.e., its syntax. Understanding the basic syntax is essential for using R for solving your pressing problem be it a simple task such as adding two numbers or performing more complex operations. This codes in this chapter are simplified one-liners and are easy to follow. With the goal being to give you a feel of the language, it will cover basic operations including arithmetic operations such as, addition, subtraction, division, and the likes, comparison operations which involves comparing two values.
Operations such as addition, subtraction, multiplication and so on, are easy to execute in R. The arithmetic operators are given below, they are the same as symbols used in elementary mathematics with only few changes:
Operator | Definition |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
^ | Exponent or Power |
** | Exponent or Power |
%% | Remainder division |
%/% | Integer division |
Run the following commands in your console:
2 + 1
[1] 3
3 - 1
[1] 2
4 * 2
[1] 8
15 / 5
[1] 3
3 ^ 2
[1] 9
3 ** 2
[1] 9
The above operations are straightforward. %%
operator returns the remainder when two numbers are divided. While %/%
returns the whole number or integer and discard the remainder.
5 %% 3
[1] 2
5 %/% 2
[1] 2
R also follows the order of operations.
5 * 15 + 14 / 7
[1] 77
The order of operation here is division, 14 / 7 = 2
, followed by multiplication, 5 * 15 = 75
, then addition of the values from the former operations 75 + 2
which is evaluated to give a total of 77. To avoid confusion, a convenient way to go about how you want your numbers to be evaluated is by using parenthesis () to block codes.
3 + (5 - 2)
[1] 6
5 * 4) / (7 - (2 + 4)) (
[1] 20
Can you notice the space between numbers and the operators. While not required, such spacing makes your code readable. Don’t be surprised that a lot of times you will read your codes. Also, if you work with in a team or want to share your work, people will be reading your codes.
Comparison operators are as their names imply, they are used to compare values. The result from their operation returns either TRUE or FALSE. You will get more information on this when we get to Chapter 3 in the next chapter.
Comparison operator in R include
Symbol | Meaning |
---|---|
> | Greater than |
< | Less than |
== | Equal to |
>= | Greater than or equal to |
<= | Less than or equal to |
!= | Not equal to |
Try these commands in R’s console:
4 > 5
[1] FALSE
5^2) < (5 * 2) (
[1] FALSE
10 >= 7
[1] TRUE
10 == 12
[1] FALSE
Multiple comparisons can be combined using operator &
(and), |
(or), and !
(not).
&
) operator returns TRUE when all conditions on both its sides of are TRUE. All other conditions returns FALSE&
(And) operator
Conditions | result |
---|---|
TRUE & TRUE | TRUE |
TRUE & FALSE | FALSE |
FALSE & TRUE | FALSE |
FALSE & FALSE | FALSE |
28 > 10 & 50 > 40
[1] TRUE
20 < 10 & 50 < 40
[1] FALSE
20 > 10 & 50 < 40
[1] FALSE
|
) returns TRUE when one of the conditions on both sides of it are TRUE.|
(OR) operator
Conditions | result |
---|---|
TRUE | FALSE |
TRUE |
FALSE | TRUE |
TRUE |
TRUE | TRUE |
TRUE |
FALSE | FALSE |
FALSE |
20 > 15 | 30 > 10
[1] TRUE
20 < 15 | 30 < 10
[1] FALSE
You can also use parentheses to enclose conditions when testing multiple conditions.
20 < 15) | (30 > 10) (
[1] TRUE
!
is used to negate, and returns the opposite of a result. This is an operator that can be very useful in complex operations. You will see this being used as we continue.!
operator
Conditions | result |
---|---|
!TRUE | FALSE |
!FALSE | !TRUE |
!TRUE
[1] FALSE
!((20 < 15) | (30 > 10))
[1] FALSE
The assignment operator is used to bind values to names/labels called variables. These names, variables, are used to reference values.
Symbol | Meaning |
---|---|
<- | Assignment / bind to value |
= | Assignment / bind to value |
-> | Rightward assignment / bind left value to the right variable |
For example, we can assign the value 17
to the variable c
. Whenever c
is called, the value 17 is returned.
<- 17
c c
[1] 17
This does not mean that the value(s) assigned to a particular variable cannot be changed. They can be changed easily by assigning a new value to the name.
<- "man" c
Now the value man
is assigned to c
using <-
operator. This is the convention used for assigning objects to variables in R. We can assign values to variables using other operators too, but they have specific use. For example while we can assign the value 23
to the variable weight using =
as shown below. The =
operator is best used within functions.
= 23
weight weight
[1] 23
::: Functions are collections of codes that perform certain actions, more on that in Chapter 4. :::
For ->
operator, the variable is at the right side of the operator.
20 * 20 -> plot_size
plot_size
[1] 400
There are times when we do not want R to executes some lines or text when writing our analysis code. We can instruct the program to skip these lines by placing the #
symbol followed by these lines or texts. These lines are called comments. Comments are essential to in-code documentation. They make our code easier to understand, as we can explain why a code is written in a certain way. This is very important when we collaborate with others or to help the understanding of our codes when we review/read them in the future. For example
<- 15 # 15 here is in kg instead of g
x # the line here is a comment
x
[1] 15
We can perform simple operations with the operators covered in this chapter. You recently got a job at the forestry research institute and you’ve been assigned to a department where you work with indigenous trees. In your department, the local/common names and not scientific names are used to identify indigenous trees. You’ve been instructed by your supervisor to go collect data on one of the experiment your team has been working on, and its data collection time. The research your team is working on needs data on the dry weights of the stem sections of four different species. You go to the lab, measure the dry weight of the tree stems, recorded them on your recording sheet and head back to your office to fire up your computer. As a R beginner, you quickly setup R and you want to see if it’ll improves your research workflow overtime. You load R up and input the data with the knowledge you just gained in your most recent lesson (Operators in R). You assign the values of the dry weight of each species to their respective name:
Trees used in this example is native to Africa.
# The dry weight in kg/m3
<- 900
ekki <- 700
moabi <- 240
obeche <- 450 iroko
Your supervisor moves in and ask you to get the difference between the dry weight of ekki
and obeche
, and the difference between the dry weight of iroko
and moabi
.
- obeche ekki
[1] 660
- moabi iroko
[1] -250
Within seconds you said the difference between the dry weight of Ekki and Obeche is 660kg
and that of Iroko and Moabi is -250
. This is quite easy to follow for you since you understand that the command you gave above have the variables holding values you assigned to it earlier, thus representing 900 - 240
and 450 - 700
.
Surprised to hear a negative value, your supervisor ask if iroko
is greater than obeche
. You executed the command quickly, making use of the greater than comparison operator
> obeche iroko
[1] TRUE
You said yes, and continued in your response stating that the result you gave earlier was the difference between Iroko and Moabi. Realizing the error, your supervisor said he intended the difference between Iroko and Obeche, and the difference between Ekki and Moabi. You executed the new command and gave the result swiftly using R as a basic calculator.
In this chapter, you’ve been introduced to some basic R syntax and the operators. The arithmetic operators are used for mathematical operations, such as addition, multiplication, and division to mention a few. We covered the comparison operator which include <
, <=
, ==
, >=
and >
for comparing values as well as their combination with logical operators like &
and |
.
Lastly, we covered the assignment operator used to reference or bind values to names called variable. When we assign a different value to the same variable name in R, it gets replaced by the new value, thus a variable can only hold one value at a time.
Now that we’ve got a good understanding of some of the basic R syntax and the operators, we will move on to Chapter 3 to learn about the data types in R.