Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is a Matrix? | Matrices
R Introduction: Part II
course content

Зміст курсу

R Introduction: Part II

R Introduction: Part II

1. Matrices
2. Data Frames
3. Lists

book
What is a Matrix?

A matrix is a two-dimensional data structure in R, which means that every matrix element can be characterized by its position among rows and columns. Matrices in R can contain data of only one type, i.e., a matrix can not contain numbers and text simultaneously.

There are several ways to create a matrix in R. On one side, you can 'merge' two rows; on the other, you can 'merge' two columns. Let's consider each method separately. The first one is the rbind() function. Function name comes from Rows Bind - this function connects vectors as rows.

12345
# Rows row_1 = c(1, 2, 3) row_2 = c(4, 5, 6) # Merge two rows rbind(row_1, row_2)
copy

As you can see, this matrix has two rows and three columns (since each row has three values). Another way - is to use cbind() function. Likewise, the previous function, name of this function comes from Columns Bind. Let's look at the example.

12345
# Columns col_1 = c(1, 2, 3) col_2 = c(4, 5, 6) # Merge two columns cbind(col_1, col_2)
copy

As you can see, this matrix has two columns and three rows (since each column has three values).

Завдання
test

Swipe to show code editor

Practice creating matrices by yourself using both methods!

  1. Create two vectors: a with integers from 1 to 4, and b with integers from 5 to 8 using the colon : sign.
  2. Create and output matrix by merging a and b as rows.
  3. Create and output matrix by merging a and b as columns.

Once you've completed this task, click the button below the code to check your solution.

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

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

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

Секція 1. Розділ 1
toggle bottom row

book
What is a Matrix?

A matrix is a two-dimensional data structure in R, which means that every matrix element can be characterized by its position among rows and columns. Matrices in R can contain data of only one type, i.e., a matrix can not contain numbers and text simultaneously.

There are several ways to create a matrix in R. On one side, you can 'merge' two rows; on the other, you can 'merge' two columns. Let's consider each method separately. The first one is the rbind() function. Function name comes from Rows Bind - this function connects vectors as rows.

12345
# Rows row_1 = c(1, 2, 3) row_2 = c(4, 5, 6) # Merge two rows rbind(row_1, row_2)
copy

As you can see, this matrix has two rows and three columns (since each row has three values). Another way - is to use cbind() function. Likewise, the previous function, name of this function comes from Columns Bind. Let's look at the example.

12345
# Columns col_1 = c(1, 2, 3) col_2 = c(4, 5, 6) # Merge two columns cbind(col_1, col_2)
copy

As you can see, this matrix has two columns and three rows (since each column has three values).

Завдання
test

Swipe to show code editor

Practice creating matrices by yourself using both methods!

  1. Create two vectors: a with integers from 1 to 4, and b with integers from 5 to 8 using the colon : sign.
  2. Create and output matrix by merging a and b as rows.
  3. Create and output matrix by merging a and b as columns.

Once you've completed this task, click the button below the code to check your solution.

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

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

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

Секція 1. Розділ 1
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt