Skip to main content
eLearner.app
Module 1 · Lesson 2 of 22/10 in the course~12 min
Module lessons (2/2)

Data Types and Matrices

R has several basic data types that allow storing different information inside vectors and variables.

Elementary Data Types

The three main data types used most frequently in R are:

  1. numeric: Decimal numbers (e.g., 4.5, 10.0) or integers.
  2. character: Text strings enclosed by double or single quotes (e.g., "Alice", 'Data').
  3. logical: Boolean values, which can only be TRUE or FALSE. In R, you can also use the abbreviations T or F (though it is recommended to use the full versions for clarity).
Code
# Examples of different types
score <- 95.5          # numeric
player_name <- "Marco"  # character
is_winner <- TRUE       # logical

Matrices

A matrix in R is a two-dimensional extension of a vector. Like vectors, matrices can only contain elements of a single type (usually numbers).

You create a matrix using the matrix() function, providing a vector of data and specifying the number of rows (nrow) or columns (ncol):

Code
# Create a 2x3 matrix with values from 1 to 6
mat <- matrix(1:6, nrow = 2, ncol = 3)
print(mat)

By default, R populates the matrix column by column.

Accessing Elements

To access a specific element in a matrix, use square brackets [row, column]:

Code
# Extract the element at the first row, second column
value <- mat[1, 2]

# Extract an entire row by leaving the column field empty
first_row <- mat[1, ]

# Extract an entire column
second_col <- mat[, 2]

Factors

Factors in R are special data structures used to represent categorical variables (qualitative data that belong to a finite set of categories, such as gender or education level). They are highly important in statistical analysis:

Code
sizes <- c("M", "L", "M", "S")
size_factor <- factor(sizes)
print(size_factor) # Also shows the Levels: S M L

Combining Data: cbind and rbind

We can combine vectors or matrices by binding them by column with cbind() (column bind) or by row with rbind() (row bind):

Code
v1 <- c(1, 2)
v2 <- c(3, 4)

# Creates a 2x2 matrix by placing v1 and v2 side-by-side as columns
combined <- cbind(v1, v2)

Try it yourself

Exercise#r.m1.l2.e1
Attempts: 0Loading…

Declare a logical variable named is_active set to TRUE and a character variable named user_name set to 'Alice'.

Loading editor…
Show hint

Usa l'operatore <- per entrambe le variabili su righe separate.

Solution available after 3 attempts

Exercise#r.m1.l2.e2
Attempts: 0Loading…

Create a matrix named mat containing the values from 1 to 6, structured with 2 rows (nrow = 2) and 3 columns (ncol = 3).

Loading editor…
Show hint

Usa la funzione matrix(1:6, nrow = 2, ncol = 3)

Solution available after 3 attempts

Exercise#r.m1.l2.e3
Attempts: 0Loading…

Given the already created matrix mat, extract the element located at the first row and third column, and save it in the variable val.

Loading editor…
Show hint

Accedi all'elemento usando le parentesi quadre: val <- mat[1, 3]

Solution available after 3 attempts

Exercise#r.m1.l2.e4
Attempts: 0Loading…

Create a factor from the vector categories and assign it to the variable cat_factor.

Loading editor…
Show hint

Use the factor() function: cat_factor <- factor(categories)

Solution available after 3 attempts

Exercise#r.m1.l2.e5
Attempts: 0Loading…

Given the vectors col1 and col2, combine them as columns of a matrix using cbind() and assign the result to my_matrix.

Loading editor…
Show hint

Use: my_matrix <- cbind(col1, col2)

Solution available after 3 attempts