Here, we will show operations in R such as assignment, arithmetic, relational, Boolean, and logical operations with examples and notes on their usage.
These operations can be performed on different data structures such as vectors, matrices, and arrays, including dates and times.
See also operations and relations on plots.
First, we discuss the assignment operations which are performed with the commonly used left assignment operator and the less common right assignment operator.
Examples are provided in the table below.
Symbol | Usage | Examples |
= |
Left assignment; most used |
[1] 1 |
<- |
Left assignment |
[1] 2 3 |
<<- |
Left assignment; *mostly used in functions |
[1] 4 |
-> |
Right assignment |
[1] 2 |
->> |
Right assignment; *mostly used in functions |
[1] 2 3 |
For arithmetic operations, these are performed with addition, subtraction, multiplication, division, exponentiation operators and some of the less common ones.
Examples are provided in the table below.
Symbol | Usage | Examples |
+ |
Addition |
[1] 5 |
- |
Subtraction |
[1] 4 |
* |
Multiplication |
[1] 10 |
/ |
Division |
[1] 2.333333 1.500000 |
^ |
Exponentiation or power |
[1] 1024 |
** |
Exponentiation or power |
[1] 25 |
x%/%y |
Integer division of \(x\) by \(y\) |
[1] 3 |
x%%y |
\(x\) Modulo \(y\) For remainder after division |
[1] 1 |
sqrt(x) |
Square root of \(x\) Returns positive number |
[1] 5 |
abs(x) |
Absolute value of \(x\) |
[1] 4.5 |
exp(x) |
Exponentiation of the number \(e\) \((=2.718282)\) by \(x\) |
[1] 2.718282 |
log(x, n) |
Logarithm of \(x\), base \(n\) *Default \(n=e\) |
[1] 3 [1] 1 |
floor(x) |
Largest integer less than \(x\) |
[1] 5 [1] -6 |
ceiling(x) |
Smallest integer greater than \(x\) |
[1] 7 [1] -6 |
round(x, n) |
Rounds \(x\) to \(n\) decimal places *Default \(n=0\) |
[1] 5.73 [1] 6 |
signif(x, n) |
Rounds \(x\) to \(n\) significant digits |
[1] 5.7 |
trunc(x) |
Integer part of \(x\) |
[1] 5 [1] -5 |
|
Decimal part of \(x\) |
[1] 0.6 [1] 0.6 |
sin(x) |
Sine of \(x\), with angles in radians (\(pi=3.141593\)) |
[1] 1 |
cos(x) |
Cosine of \(x\), with angles in radians |
[1] 1 |
tan(x) |
Tangent of \(x\), with angles in radians (\(pi=3.141593\)) |
[1] 1 |
asin(x) |
Inverse sine of \(x\), (\(pi/2=1.570796\)) |
[1] 1.570796 |
acos(x) |
Inverse cosine of \(x\) |
[1] 0 |
atan(x) |
Inverse tangent of \(x\), (\(pi/4=0.7853982\)) |
[1] 0.7853982 |
Note the following:
log(x, e) = log(x)
since \(e\) is the default value of \(n\). Similarly,
round(2.1, 0) = 2 = round(2.1)
since \(0\) is the default value of \(n\).round(x, n)
rounds 0.5 to an even number
when set to \(0\) decimal places. For
example round(5.5, 0) = 6
and
round(6.5, 0) = 6
.For relational or comparison operations that return TRUE
or FALSE
values that are performed with greater
than, lesser than, equal to and not equal
to relational operators.
Examples are provided in the table below.
Symbol | Usage | Examples |
> |
Greater than? |
[1] FALSE |
< |
Lesser than? | x = 3 y = 7
[1] TRUE |
>= |
Greater than or equal to? |
[1] FALSE |
<= |
Lesser than or equal to? |
[1] FALSE TRUE TRUE |
== |
Equal to? |
[1] TRUE |
!= |
Not equal to? |
[1] TRUE FALSE FALSE |
Note the following:
2.001-2
will return 0.001
but
2.001-2 == 0.001
will return FALSE
. This is
not intuitive; however, it is because in R, numbers including floating
points are represented in binary format which leads to some numerical
errors. These errors are approximated away when converted back to
decimal format. To highlight this binary conversion error:
print(2.001-2, digits = 16)
returns
0.0009999999999998899
not 0.001
.For logical or Boolean operations that return TRUE
or
FALSE
values that are performed with AND,
OR, and negation logical operators.
Examples are provided in the table below.
Symbol | Usage | Examples |
x | y |
x OR y: Checks element-wise in vector. |
[1] TRUE FALSE |
x || y |
x Logical OR y: Checks two statements. |
[1] FALSE |
x & y |
x AND y: Checks element-wise in vector. |
[1] FALSE TRUE |
x && y |
x Logical AND y: Checks two statements. |
[1] TRUE |
!x |
Not x or negation logic of x |
[1] TRUE FALSE TRUE |
xor(x, y) |
Is at least one of x or y true? |
[1] TRUE |
isTRUE(x) |
Is x true? |
[1] TRUE |
isFALSE(x) |
Is x false? |
[1] FALSE |
Besides the operations and operators above, there are two commonly used ones, the colon operator, and the value matching operator.
Examples are provided in the table below.
Symbol | Usage | Examples |
a:b |
Returns numbers from a to b |
[1] 1 2 3 4 5 6 7 |
x %in% y |
Checks for matches of x in y |
[1] TRUE FALSE |
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes