Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Operation with Matrices | Matrices
R Introduction: Part II
course content

Contenido del Curso

R Introduction: Part II

R Introduction: Part II

1. Matrices
2. Data Frames
3. Lists

bookOperation with Matrices

Good! Now we know how to create and customize matrices. It's time to consider the operations we can perform with matrices.

You can perform basic math operations with matrices. Performing a basic operation between a matrix and a single number will perform the respective operation for all matrix elements. For example, for the given matrix below...

...we can multiply each element by 3.

1234567
# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Multiply each matrix element by 3 m * 3
copy

As you can see, each matrix element was multiplied by 3. You can also use mean() and sum() functions for matrices. These functions will return the overall mean or total value, respectively. For example,

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Calculate overall mean mean(m)
copy

Additionally, you can also apply these functions to rows or columns separately. These functions are rowSums(), rowMeans(), colSums(), colMeans(). I think it's obvious what each function does according to their names. For example, let's calculate the column sums.

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Columns sums colSums(m)
copy

Let's practice a bit.

Tarea
test

Swipe to show code editor

Given matrix named m.

Your tasks are:

  1. Divide each element by 2, then add 1. Reassign the result to the m variable.
  2. Output the matrix m.
  3. Output the columns totals of the m matrix.
  4. Output mean value of all m elements.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 6
toggle bottom row

bookOperation with Matrices

Good! Now we know how to create and customize matrices. It's time to consider the operations we can perform with matrices.

You can perform basic math operations with matrices. Performing a basic operation between a matrix and a single number will perform the respective operation for all matrix elements. For example, for the given matrix below...

...we can multiply each element by 3.

1234567
# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Multiply each matrix element by 3 m * 3
copy

As you can see, each matrix element was multiplied by 3. You can also use mean() and sum() functions for matrices. These functions will return the overall mean or total value, respectively. For example,

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Calculate overall mean mean(m)
copy

Additionally, you can also apply these functions to rows or columns separately. These functions are rowSums(), rowMeans(), colSums(), colMeans(). I think it's obvious what each function does according to their names. For example, let's calculate the column sums.

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Columns sums colSums(m)
copy

Let's practice a bit.

Tarea
test

Swipe to show code editor

Given matrix named m.

Your tasks are:

  1. Divide each element by 2, then add 1. Reassign the result to the m variable.
  2. Output the matrix m.
  3. Output the columns totals of the m matrix.
  4. Output mean value of all m elements.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 6
toggle bottom row

bookOperation with Matrices

Good! Now we know how to create and customize matrices. It's time to consider the operations we can perform with matrices.

You can perform basic math operations with matrices. Performing a basic operation between a matrix and a single number will perform the respective operation for all matrix elements. For example, for the given matrix below...

...we can multiply each element by 3.

1234567
# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Multiply each matrix element by 3 m * 3
copy

As you can see, each matrix element was multiplied by 3. You can also use mean() and sum() functions for matrices. These functions will return the overall mean or total value, respectively. For example,

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Calculate overall mean mean(m)
copy

Additionally, you can also apply these functions to rows or columns separately. These functions are rowSums(), rowMeans(), colSums(), colMeans(). I think it's obvious what each function does according to their names. For example, let's calculate the column sums.

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Columns sums colSums(m)
copy

Let's practice a bit.

Tarea
test

Swipe to show code editor

Given matrix named m.

Your tasks are:

  1. Divide each element by 2, then add 1. Reassign the result to the m variable.
  2. Output the matrix m.
  3. Output the columns totals of the m matrix.
  4. Output mean value of all m elements.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Good! Now we know how to create and customize matrices. It's time to consider the operations we can perform with matrices.

You can perform basic math operations with matrices. Performing a basic operation between a matrix and a single number will perform the respective operation for all matrix elements. For example, for the given matrix below...

...we can multiply each element by 3.

1234567
# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Multiply each matrix element by 3 m * 3
copy

As you can see, each matrix element was multiplied by 3. You can also use mean() and sum() functions for matrices. These functions will return the overall mean or total value, respectively. For example,

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Calculate overall mean mean(m)
copy

Additionally, you can also apply these functions to rows or columns separately. These functions are rowSums(), rowMeans(), colSums(), colMeans(). I think it's obvious what each function does according to their names. For example, let's calculate the column sums.

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Columns sums colSums(m)
copy

Let's practice a bit.

Tarea
test

Swipe to show code editor

Given matrix named m.

Your tasks are:

  1. Divide each element by 2, then add 1. Reassign the result to the m variable.
  2. Output the matrix m.
  3. Output the columns totals of the m matrix.
  4. Output mean value of all m elements.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 1. Capítulo 6
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt