[1] "John"
[1] "Tom"
Bioinformatics Core Facility CECAD
2026-05-11
Session 2 :: Basic Concepts in R
<- or = can be used for assigning a valueVariable names may be short (like x and y) or descriptive (age, carname, total_volume).
| Data Type (Class) | Example | Verify | value |
|---|---|---|---|
| Character | “Hello!” | x<-"Hello!" print(x) class(x) |
Hello! character |
| Numeric (double) | 1.3, 5, 4.2 |
|
1.35 |
| Numeric (integer) | 1L, 0L, 4L |
|
35 |
| Complex | 2+3i | x<-2+3i print(x) class(x) |
2+3i complex |
| Logical | TRUE / FALSE | x<-TRUE print(x) class(x) |
TRUE logical |
Variables are assigned with R-Objects and the data types of the R-objects become the data types of the variables. There are many types of R-objects. The frequently used ones are −
[1] "100" "200" "450" "670"
, , 1
[,1] [,2] [,3]
[1,] 2 10 -4.0
[2,] 0 7 5.5
, , 2
[,1] [,2] [,3]
[1,] 4 12 -2.0
[2,] 2 9 7.5
Lists are vectors of a special kind. Think of their elements as “hooks”:
Each hook carries an R object:
The “prey” carried by a “hook” may be of arbitrary data type.
Specifically, a list element may "hook" another list:
This feature allows to construct complex objects, tailored to specific needs, and store them in variables for re-use.
Operators are the symbols that tell the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and provides the following types of operators −
| Operator | Name | Example |
|---|---|---|
| + | Addition | x + y |
| - | Subtraction | x - y |
| * | Multiplication | x * y |
| / | Division | x / y |
| ^ | Exponent | x ^ y |
| %% | Modulus (Remainder from division) | x %% y |
| %/% | Integer Division | x%/%y |
| Operator | Name | Example |
|---|---|---|
| == | Equal | x == y |
| != | Not equal | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
| Operator | Description |
|---|---|
| & | Element-wise Logical AND operator. It returns TRUE if both elements are TRUE |
| && | Logical AND operator - Returns TRUE if both statements are TRUE |
| | | Elementwise- Logical OR operator. It returns TRUE if one of the statement is TRUE |
| || | Logical OR operator. It returns TRUE if one of the statement is TRUE. |
| ! | Logical NOT - returns FALSE if statement is TRUE |
| Operator | Description | Example |
|---|---|---|
| : | Creates a series of numbers in a sequence | x <- 1:10 |
| %in% | Find out if an element belongs to a vector | x %in% y |
| %*% | Matrix Multiplication | x <- Matrix1 %*% Matrix2 |
Unlike many other programming languages, R does not distinguish between an item of a data type (e.g., a single numeric value) and a vector of length one of the same type.
Almost all R operators and many R functions are vectorized, that is
This allows for very compact notation:
If the input vectors of a vectorized operation are not equal in length, then a behavior kicks in which can cause bugs which are hard to find: shorter input vectors are simply repeated until the length of the longest vector is reached.
[1] "A|u|X" "B|v|X" "C|u|X" "D|v|X" "E|u|X"
The example shows that a vector of length 1 is paired with every element of the output. This can be exploited to add or multiply a single number to/with all elements of a vector: