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 performs arithmetic easily and 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
Notice the spaces between numbers in arithmetic operations. While not required, spacing makes the code more readable. Don’t be surprised that a lot of times you would try to figure out what you have done and also why you did particular thing. 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 is either TRUE or FALSE. TRUE and FALSE are logical value. 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 falseConditions | 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 and its adjoining result
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 tree_height 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
tree_height tree_height
[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.
Perform the following calculations in R:
Assign the result of (80 + 5) * 3
(total seeds planted and their multiplication) to a variable named total_seeds
. What is the value of total_seeds
?
If x = 150
(representing trees in plot A) and y = 30
(representing trees removed due to disease), calculate the following:
Assign the value 25
(representing the number of endangered species spotted) to a variable named species
. Now, assign the sum of species
and 12
(representing newly discovered species) to a variable named total_species
. What is the value of total_species
?
Use the ->
operator to assign the value 350
(representing crop yield in kg) to a variable named yield
. Verify the value of yield
.
Assign the value of total_species
(from Question 4) to a new variable named species_count
using the <-
operator.
Consider the following variables: tree_height1 = 15
(meters) and tree_height2 = 12
. Using comparison operators, answer whether the following statements are TRUE or FALSE:
Assign values fertilizer_applied = 200
(kg/ha) and recommended_fertilizer = 250 (kg/ha)
. Write R commands to:
fertilizer_applied
is less than recommended_fertilizer
.fertilizer_applied
is equal to recommended_fertilizer
.recommended_fertilizer
is greater than or equal to fertilizer_applied
. ### Part 4: Logical Operators (&, |, !) {.unnumbered}Consider the following variables: irrigation = TRUE
and pesticide_use = FALSE
. Evaluate the following expressions:
If water_level = 8
(meters) and soil_moisture = 5
(index value), check whether:
Create a logical expression using the & operator that returns TRUE
if both conditions are met:
Write an R command that evaluates whether either crop_yield > 300
(kg) or rainfall < 20
(mm) using the | operator.