Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте matrix() Function | Section
Essential R Programming for Absolute Beginners - 1768563985826

bookmatrix() Function

The matrix() function creates a matrix from a single vector. It allows you to specify how many rows and columns the matrix should have, and whether it should be filled by rows or by columns.

Function Overview

matrix(data = NA, nrow = 1, ncol = 1, byrow = F)
  • data: the vector used to fill the matrix;
  • nrow: number of rows;
  • ncol: number of columns;
  • byrow: if TRUE, the matrix is filled row by row; if FALSE - column by column.
Note
Note

The length of the vector must be divisible by either nrow or ncol. If both are specified, then nrow * ncol must equal the vector length.

Example

12345678
# Vector of integers num <- 1:9 # Fill by columns (default) matrix(num, nrow = 3, ncol = 3) # Fill by rows matrix(num, nrow = 3, ncol = 3, byrow = T)
copy

The first matrix is filled column by column (default configuration), while the second one is filled row by row.

Note
Study More

The seq() function can generate ranges similarly to a colon (:):

  • seq(a, b) generates integers from a to b;
  • seq(a, b, c) generates integers from a to b with step size c.
Завдання

Swipe to start coding

You have 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 the matrix() function to do this.

Рішення

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 28
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

close

bookmatrix() Function

Свайпніть щоб показати меню

The matrix() function creates a matrix from a single vector. It allows you to specify how many rows and columns the matrix should have, and whether it should be filled by rows or by columns.

Function Overview

matrix(data = NA, nrow = 1, ncol = 1, byrow = F)
  • data: the vector used to fill the matrix;
  • nrow: number of rows;
  • ncol: number of columns;
  • byrow: if TRUE, the matrix is filled row by row; if FALSE - column by column.
Note
Note

The length of the vector must be divisible by either nrow or ncol. If both are specified, then nrow * ncol must equal the vector length.

Example

12345678
# Vector of integers num <- 1:9 # Fill by columns (default) matrix(num, nrow = 3, ncol = 3) # Fill by rows matrix(num, nrow = 3, ncol = 3, byrow = T)
copy

The first matrix is filled column by column (default configuration), while the second one is filled row by row.

Note
Study More

The seq() function can generate ranges similarly to a colon (:):

  • seq(a, b) generates integers from a to b;
  • seq(a, b, c) generates integers from a to b with step size c.
Завдання

Swipe to start coding

You have 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 the matrix() function to do this.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 28
single

single

some-alt