The R syntax is quite intuitive. An R function always must be written with parentheses, even if there is nothing to declare within them:
print("hello")
sum(2, 7, 10)
and functions can be nested with each other expressing them in the proper way:
sum(2, 7, sum(8, 2))
Every function in R can contain different arguments susceptible to setting. The different adjustable arguments of an R function are declared and separated by coma, and restricted by their expected attributes:
seq(1, 100, 5)
In R languaje, an object is created using the assign operator, which adopt the written form of an arrow combining the minus symbol and the greater than (->) or less than (<-) symbols:
from right to left
mydata <- seq(1:10)
or from left to right
seq(1:10) - > mydata
The conventional syntax of R scripting is right-to-left. Every object created in R is stored in the active memory of the computer linked to an unique name. WARNING: If the object already exists, its value or content will be erased and replaced with the lastest version created.
In addition to the assignment operators (<- or ->), R use a notable number of operators to make arithmetic, comparisons and evaluate conditional statements.
R arithmetic operators
Operator | Name | Example |
---|---|---|
+ | addition | a + b, var1 + var2 |
- | subtraction | a - b, var1 - var2 |
* | multiplication | a * b, var1 * var2 |
/ | division | a / b, var1 / var2 |
^ | exponent | a ^ b, var1 ^ var2 |
R comparison operators
Operator | Name | Example |
---|---|---|
== | equal | a == b, var1 == var2 |
!= | not equal | a != b, var1 != var2 |
> | greater than | a > b, var1 > var2 |
< | less than | a < b, var1 < var2 |
>= | greater than or equal to | a >= b, var1 >= var2 |
<= | less than or equal to | a <= b, var1 <= var2 |
WARNING. In R, == is a logical operator for comparison, while = can be used for assignment as well).
R logical operators
Operator | Name | Example |
---|---|---|
& | Logical AND operator. Returns TRUE if both the operands are TRUE | a > b & var1 > var2 |
| | Logical OR operator. Returns TRUE if either of the operands is TRUE | a > b | var1 > var2 |
! | Logical NOT operator. Returns FALSE if statement is TRUE | ! var1 > var2 |
Miscellaneous operators
Operator | Name | Example |
---|---|---|
: | Creates a number series in a sequence | 1 : 25 |
%in% | Evaluate if an element belongs to a vector | a %in% b |
We already know R works with objects, characterized by their names and their content, but they also have attributes which indicate the type of data represented. All R objects have two intrinsic attributes: mode and length. The mode is the basic type of the elements of the object (similar to class); there are four main modes:
On the other hand, the length is the number of elements of the object. Both attributes can be inspected on any R object using the functions mode and length, respectively:
mydata <- seq(1:25)
mydata
mode(mydata)
length(mydata)
mydata <- c("high", "medium", "low")
mydata
mode(mydata)
length(mydata)
IMPORTANT: Missing data, of any mode, are represented by NA (not available). R can represents non-finite numeric values with Inf and -Inf, or values which are not numbers with NaN (not a number).
a <- 9/0
a
a - a
Naming objects in R is flexible, BUT there are a few rules to consider to avoid errors during object creation, data loading, and function calling:
Suppose the following R objects:
a <- c(5, 89, 63, 15, 46, 75, 25, 42)
b <- c(84, 15, 24, 64, 2, 90, 42, 17)
Then use arithmetic and comparison operators on both objects and examine the resulting outputs. Also, try to explore their attributes.