2 + 1
[1] 3
3 - 1
[1] 2
4 * 2
[1] 8
15 / 5
[1] 3
3 ^ 2
[1] 9
3 ** 2
[1] 9
Understanding the basic syntax of R is essential for performing complex operations. Basic operations in R include arithmetic operations such as, addition, and subtraction, comparison operations which involves the use of conditionals to check how object compares against one another.
Operations such as addition, subtraction, multiplication and so on, are easy to execute in R. The arithmetic operators are given below:
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 called variables. Variables are names that 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 the result.
<- 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 the new value to the same name.
<- "man" c
Above we assigned the new value man
to c
using <-
. 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, which are collections of codes that perform certain actions, more on that in Chapter 4.
= 23
weight weight
[1] 23
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. For example we can compare the average dry weight of different African tree species.
# The dry weight in kg/m3
<- 1065
ekki <- 860
moabi <- 380
obeche <- 660 iroko
We can get the difference between ekki
and obeche
, and between iroko
and moabi
.
- obeche ekki
[1] 685
- moabi iroko
[1] -200
The command above translate to 1065 - 380
and 660 - 860
respectively. We can also use the comparison operators on this variables.
< iroko ekki
[1] FALSE
< ekki moabi
[1] TRUE
> obeche ekki
[1] TRUE
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.