Bài học theo mô-đun (2/2)
Các kiểu dữ liệu và ma trận
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:
numeric: Decimal numbers (e.g.,4.5,10.0) or integers.character: Text strings enclosed by double or single quotes (e.g.,"Alice",'Data').logical: Boolean values, which can only beTRUEorFALSE. In R, you can also use the abbreviationsTorF(though it is recommended to use the full versions for clarity).
# 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):
# 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]:
# 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:
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):
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
Declare a logical variable named is_active set to TRUE and a character variable named user_name set to 'Alice'.
Hiển thị gợi ý
Use the <- operator for both variables on separate lines.
Giải pháp khả dụng sau 3 lần thử
Create a matrix named mat containing the values from 1 to 6, structured with 2 rows (nrow = 2) and 3 columns (ncol = 3).
Hiển thị gợi ý
Use the matrix(1:6, nrow = 2, ncol = 3) function.
Giải pháp khả dụng sau 3 lần thử
Given the already created matrix mat, extract the element located at the first row and third column, and save it in the variable val.
Hiển thị gợi ý
Access the element using square brackets: val <- mat[1, 3]
Giải pháp khả dụng sau 3 lần thử
Create a factor from the vector categories and assign it to the variable cat_factor.
Hiển thị gợi ý
Use the factor() function: cat_factor <- factor(categories)
Giải pháp khả dụng sau 3 lần thử
Given the vectors col1 and col2, combine them as columns of a matrix using cbind() and assign the result to my_matrix.
Hiển thị gợi ý
Use: my_matrix <- cbind(col1, col2)
Giải pháp khả dụng sau 3 lần thử