Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen matrix() Function | Matrices
Einführung in R: Teil I

bookmatrix() 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:

matrix(data = NA, nrow = 1, ncol = 1, byrow = F)

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 the data parameter must be divisible by nrow or ncol. If both parameters are set, then nrow*ncol must equal the vector length.

For example, let's construct a 3x3 matrix with integers from 1 to 9.

12345
# Vector of integers num <- 1:9 # Build a matrix from vector matrix(num, nrow = 3, ncol = 3)
copy

As you can see, this matrix was built from above to below first. Let's set parameter byrow to T and compare the results.

123
num <- 1:9 # Build a matrix from vector by rows matrix(num, nrow = 3, ncol = 3, byrow = T)
copy

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.

Aufgabe

Swipe to start coding

Given a vector of numbers named num.

2  4  6  8 10 12 14 16

Based on this vector, you need to build the following matrix.

 2  4  6  8
10 12 14 16

Use only the matrix() function, and think about the correct values of parameters.

Lösung

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.

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain more about the `byrow` parameter and how it affects the matrix?

What happens if the length of the vector isn't divisible by the number of rows or columns?

Can you show an example using the `seq()` function to create a matrix?

close

Awesome!

Completion rate improved to 2.27

bookmatrix() Function

Swipe um das Menü anzuzeigen

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:

matrix(data = NA, nrow = 1, ncol = 1, byrow = F)

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 the data parameter must be divisible by nrow or ncol. If both parameters are set, then nrow*ncol must equal the vector length.

For example, let's construct a 3x3 matrix with integers from 1 to 9.

12345
# Vector of integers num <- 1:9 # Build a matrix from vector matrix(num, nrow = 3, ncol = 3)
copy

As you can see, this matrix was built from above to below first. Let's set parameter byrow to T and compare the results.

123
num <- 1:9 # Build a matrix from vector by rows matrix(num, nrow = 3, ncol = 3, byrow = T)
copy

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.

Aufgabe

Swipe to start coding

Given a vector of numbers named num.

2  4  6  8 10 12 14 16

Based on this vector, you need to build the following matrix.

 2  4  6  8
10 12 14 16

Use only the matrix() function, and think about the correct values of parameters.

Lösung

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.

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

close

Awesome!

Completion rate improved to 2.27
Abschnitt 4. Kapitel 2
single

single

some-alt