[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).
Each R object has a data type (for object x, query it with class(x)). Variables have the type of the assigned object.
| 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 |
More complex data structures (often also called data types) are composed from the basic data types. The frequently used ones are −
A vector is a “chain” of one or more instances of one data type:
a b c d
100 200 450 670
Most mathematical R operators and many R functions are vectorizing, that is,
they can operate on vectors of any length:
What happens if we combine vectors of unequal length ?
Day
Sex 1 2 3
F 2 10 -4.0
M 0 7 5.5
, , 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.
A list element is not in itself a data value, but a "hook" which may attach an arbitrary R object.
A list can mirror the structure of a dataset in an R object.
Individual components can be extracted by index or by name:
gender height weight Age
1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26
The factor data type describes relationships between categories. A factor variable is typically used to group another variable according to these categories, and then run statistical tests of category differences on this variable.
Often, the grouping variable and the grouped variable are columns in the same dataframe.
An unordered factor distinguishes a single base category (= the base level = level 1).
Other categories are tested against this base level.
An ordered factor defines an order on all levels. This allows to test whether the trend of some variable (here: sugar content) follows this order.
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 |
|---|---|
| && | Statement-wise Logical AND operator: Returns TRUE if left and right statement is TRUE |
| & | Vectorizing Logical AND operator |
| || | Statement-wise Logical OR operator: Returns TRUE if one of the statements is TRUE. |
| | | Vectorizing Logical OR operator. |
| ! | 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 |