Contenido del Curso
R Introduction: Part II
R Introduction: Part II
matrix() Function
Sometimes you may have only one vector that you need to convert into a matrix. How can it be done? The answer is simple - by using the matrix()
function.
This function has the following parameters:
These are not all the parameters, but the most important for us.
data
- is the vector that we want to use to build the matrix;nrow
- number of rows in a new matrix;ncol
- number of columns in a new matrix;byrow
- logical, should the matrix be filled by rows. It's important to note that the length of the vector filled as thedata
parameter must be divisible bynrow
orncol
. If both parameters are set, thennrow*ncol
must equal the vector length.
For example, let's construct a 3x3 matrix with integers from 1 to 9.
# Vector of integers num <- 1:9 # Build a matrix from vector matrix(num, nrow = 3, ncol = 3)
As you can see, this matrix was built from above to below first. Let's set parameter byrow
to T
and compare the results.
num <- 1:9 # Build a matrix from vector by rows matrix(num, nrow = 3, ncol = 3, byrow = T)
As you can see, we filled this matrix from left to right. We were free to leave only one of the nrow
or ncol
parameters since 9 (number of elements in the vector) is divisible by 3 and returns an integer result.
Tarea
Given a vector of numbers named num
.
Based on this vector, you need to build the following matrix.
Use only the matrix()
function, and think about the correct values of parameters.
How does seq()
function work? seq(a, b)
generates integers from a
to b
inclusive. seq(a, b, c)
generates integers from a
to b
with the step c
.
¡Gracias por tus comentarios!
matrix() Function
Sometimes you may have only one vector that you need to convert into a matrix. How can it be done? The answer is simple - by using the matrix()
function.
This function has the following parameters:
These are not all the parameters, but the most important for us.
data
- is the vector that we want to use to build the matrix;nrow
- number of rows in a new matrix;ncol
- number of columns in a new matrix;byrow
- logical, should the matrix be filled by rows. It's important to note that the length of the vector filled as thedata
parameter must be divisible bynrow
orncol
. If both parameters are set, thennrow*ncol
must equal the vector length.
For example, let's construct a 3x3 matrix with integers from 1 to 9.
# Vector of integers num <- 1:9 # Build a matrix from vector matrix(num, nrow = 3, ncol = 3)
As you can see, this matrix was built from above to below first. Let's set parameter byrow
to T
and compare the results.
num <- 1:9 # Build a matrix from vector by rows matrix(num, nrow = 3, ncol = 3, byrow = T)
As you can see, we filled this matrix from left to right. We were free to leave only one of the nrow
or ncol
parameters since 9 (number of elements in the vector) is divisible by 3 and returns an integer result.
Tarea
Given a vector of numbers named num
.
Based on this vector, you need to build the following matrix.
Use only the matrix()
function, and think about the correct values of parameters.
How does seq()
function work? seq(a, b)
generates integers from a
to b
inclusive. seq(a, b, c)
generates integers from a
to b
with the step c
.
¡Gracias por tus comentarios!
matrix() Function
Sometimes you may have only one vector that you need to convert into a matrix. How can it be done? The answer is simple - by using the matrix()
function.
This function has the following parameters:
These are not all the parameters, but the most important for us.
data
- is the vector that we want to use to build the matrix;nrow
- number of rows in a new matrix;ncol
- number of columns in a new matrix;byrow
- logical, should the matrix be filled by rows. It's important to note that the length of the vector filled as thedata
parameter must be divisible bynrow
orncol
. If both parameters are set, thennrow*ncol
must equal the vector length.
For example, let's construct a 3x3 matrix with integers from 1 to 9.
# Vector of integers num <- 1:9 # Build a matrix from vector matrix(num, nrow = 3, ncol = 3)
As you can see, this matrix was built from above to below first. Let's set parameter byrow
to T
and compare the results.
num <- 1:9 # Build a matrix from vector by rows matrix(num, nrow = 3, ncol = 3, byrow = T)
As you can see, we filled this matrix from left to right. We were free to leave only one of the nrow
or ncol
parameters since 9 (number of elements in the vector) is divisible by 3 and returns an integer result.
Tarea
Given a vector of numbers named num
.
Based on this vector, you need to build the following matrix.
Use only the matrix()
function, and think about the correct values of parameters.
How does seq()
function work? seq(a, b)
generates integers from a
to b
inclusive. seq(a, b, c)
generates integers from a
to b
with the step c
.
¡Gracias por tus comentarios!
Sometimes you may have only one vector that you need to convert into a matrix. How can it be done? The answer is simple - by using the matrix()
function.
This function has the following parameters:
These are not all the parameters, but the most important for us.
data
- is the vector that we want to use to build the matrix;nrow
- number of rows in a new matrix;ncol
- number of columns in a new matrix;byrow
- logical, should the matrix be filled by rows. It's important to note that the length of the vector filled as thedata
parameter must be divisible bynrow
orncol
. If both parameters are set, thennrow*ncol
must equal the vector length.
For example, let's construct a 3x3 matrix with integers from 1 to 9.
# Vector of integers num <- 1:9 # Build a matrix from vector matrix(num, nrow = 3, ncol = 3)
As you can see, this matrix was built from above to below first. Let's set parameter byrow
to T
and compare the results.
num <- 1:9 # Build a matrix from vector by rows matrix(num, nrow = 3, ncol = 3, byrow = T)
As you can see, we filled this matrix from left to right. We were free to leave only one of the nrow
or ncol
parameters since 9 (number of elements in the vector) is divisible by 3 and returns an integer result.
Tarea
Given a vector of numbers named num
.
Based on this vector, you need to build the following matrix.
Use only the matrix()
function, and think about the correct values of parameters.
How does seq()
function work? seq(a, b)
generates integers from a
to b
inclusive. seq(a, b, c)
generates integers from a
to b
with the step c
.