R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
In R, you can easily create matrices from vectors using the matrix()
function. In this tutorial, we'll go through a step-by-step process to create matrices from vectors.
First, let's create a simple matrix from a single vector:
# Create a vector v <- c(1, 2, 3, 4, 5, 6) # Convert the vector into a matrix with 2 rows and 3 columns m <- matrix(v, nrow = 2) print(m)
This will create a matrix:
[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6
If you have multiple vectors and want to create a matrix by combining them, you can do so using the cbind()
or rbind()
functions, depending on whether you want to combine them as columns or rows, respectively:
# Create vectors v1 <- c(1, 2, 3) v2 <- c(4, 5, 6) # Create matrix by combining the vectors as columns m1 <- cbind(v1, v2) print(m1)
This will produce:
v1 v2 [1,] 1 4 [2,] 2 5 [3,] 3 6
For row-wise combination:
# Create matrix by combining the vectors as rows m2 <- rbind(v1, v2) print(m2)
Output:
[,1] [,2] [,3] v1 1 2 3 v2 4 5 6
The nrow
and ncol
arguments in the matrix()
function allow you to specify the number of rows and columns, respectively:
# Convert the vector into a matrix with 3 rows and 2 columns m3 <- matrix(v, nrow = 3, ncol = 2) print(m3)
This gives:
[,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6
By default, the matrix is filled column-wise. If you want to fill it row-wise, you can use the byrow
argument:
# Convert the vector into a matrix with 2 rows and 3 columns, filling by rows m4 <- matrix(v, nrow = 2, byrow = TRUE) print(m4)
Output:
[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6
With the tools mentioned above, you can easily create matrices from vectors in R. Adjust the arguments as per your needs to get the desired matrix structure.
R matrix creation example:
# Create vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Create a matrix using matrix() function matrix_example <- matrix(c(vector1, vector2), nrow = 2, byrow = TRUE)
Combine vectors into a matrix in R:
# Create vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Combine vectors into a matrix using cbind() function matrix_combined <- cbind(vector1, vector2)
Matrix function in R:
# Create vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Use the matrix() function to create a matrix matrix_function <- matrix(c(vector1, vector2), nrow = 2, byrow = TRUE)
Bind vectors into a matrix in R:
# Create vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Bind vectors into a matrix using rbind() function matrix_bound <- rbind(vector1, vector2)
R matrix from multiple vectors:
# Create vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Create a matrix from multiple vectors using matrix() function matrix_multiple <- matrix(c(vector1, vector2), nrow = 2, byrow = TRUE)
Create a data matrix in R:
# Create vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Create a data matrix using data.matrix() function data_matrix <- data.matrix(cbind(vector1, vector2))
Row-wise and column-wise matrix creation in R:
# Create vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Create a matrix row-wise using rbind() function matrix_rowwise <- rbind(vector1, vector2) # Create a matrix column-wise using cbind() function matrix_columnwise <- cbind(vector1, vector2)
Transpose vectors into a matrix in R:
# Create vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Transpose vectors into a matrix using t() function matrix_transposed <- t(matrix(c(vector1, vector2), nrow = 2, byrow = TRUE))
Convert vectors to matrix using cbind and rbind in R:
# Create vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) # Convert vectors to a matrix using cbind() and rbind() functions matrix_cbind_rbind <- rbind(vector1, vector2)