15  Матриці [EN]

Автор

Юрій Клебан

15.1 What is matrices?

Matrix - a collection of elements of the same type (numeric,character, logical) with a fixed set of rows and columns. In the case where the matrix has only rows and columns, it is a two-dimensional data array.

The matrix is created using the matrix() function:

matrix(1:10, byrow = TRUE, nrow = 2)
A matrix: 2 × 5 of type int
1234 5
678910

where 1:10 - a set of elements of the matrix, it can also be a pre-formed vector (entered, by calculation, from a file, etc.),byrow = TRUE - means that the elements in the matrix will be written in rows, so in the pedestrian line contains the value 1:5, and the second6:10 (if we need to write information on the lines then we should use byrow = FASLE),nrow - the number of rows of the matrix.

sales1 <- c(12, 14, 15)
sales2 <- c(22, 15, 21)
sales <- c(sales1, sales2)
m <- matrix(sales, byrow= T, nrow = 2)
m
A matrix: 2 × 3 of type dbl
121415
221521

15.2 Naming matrices

To specify the names of rows and columns of the matrix, use the functions rownames() and colnames():

m <- matrix(1:9, nrow = 3)
rownames(m) <- c("row1", "row2", "row3")
colnames(m) <- c("c1", "c2", "c3")
m
A matrix: 3 × 3 of type int
c1c2c3
row1147
row2258
row3369

15.3 Add rows and columns

Special methods cbind/rbind are used to change the number of elements in rows and columns of matrices, as well as to quickly combine them.

** The cbind ** function allows you to add one or more matrices and/or vectors behind one of the columns. That is, there is not a simple connection, but a comparison by key field. Consider an example:

m1 <- matrix(c(1:3, 101:103), nrow = 3)
colnames(m1) <- c("A", "B")

m2 <- matrix(c(201:203, 1001:1003), nrow = 3)
colnames(m2) <- c("C", "D")

m_bind <- cbind(m1, m2)

m1
m2
m_bind
A matrix: 3 × 2 of type int
AB
1101
2102
3103
A matrix: 3 × 2 of type int
CD
2011001
2021002
2031003
A matrix: 3 × 4 of type int
ABCD
11012011001
21022021002
31032031003

15.4 Access to matrix elements

The elements of the matrix are accessed by the index of rows and columns. You can select ranges in a similar way to vectors.

Let’s look at an example:

m <- matrix(11:25, nrow = 3)
m
A matrix: 3 × 5 of type int
1114172023
1215182124
1316192225

To display the 10th element of the matrix, you can use the entries (note that the account is from the right left corner of the columns):

m[10]    
m[[10]]
20
20

To display the same element using row and column indexes, write as follows:

# Row #1
# Column #4
m[1,4]
20

Question: What record should you use ti get 18?

Answer: m[2,3]

m[2,3]
18

If you want to output / use an entire row or a whole column, then the block with the index of unnecessary dimensionality can be left blank:

m[1, ] # first row only
m[c(1,3), ] # first and third row only
  1. 11
  2. 14
  3. 17
  4. 20
  5. 23
A matrix: 2 × 5 of type int
1114172023
1316192225
m[, 1] # first column only
m[, c(1,3)] # first and third column only
  1. 11
  2. 12
  3. 13
A matrix: 3 × 2 of type int
1117
1218
1319

You can also specify a list of rows and columns to be output / received simultaneously:

m[c(1,3), 2:4]
A matrix: 2 × 3 of type int
141720
161922

You can exclude individual columns or rows by using indexes with minus signs (-):

m[-1, c(-2:-3)]
A matrix: 2 × 3 of type int
122124
132225

15.5 Useful functions

15.5.1 Matrix dimmentions

To obtain information about the dimensions of the table, there are special functions: nrow(), ncol(), dim():

# Decalre matrix
m <- matrix(1:15, ncol = 3)
m

print(paste("Rows:", nrow(m)))
print(paste("Cols:", ncol(m)))

print(paste("Dim:", paste0(dim(m), collapse = " x ")))
A matrix: 5 × 3 of type int
1 611
2 712
3 813
4 914
51015
[1] "Rows: 5"
[1] "Cols: 3"
[1] "Dim: 5 x 3"

Using nrow() andncol() allows you to access the last row and column of the matrix, respectively:

m[nrow(m), ] # last row
m[, ncol(m)] # last colum
  1. 5
  2. 10
  3. 15
  1. 11
  2. 12
  3. 13
  4. 14
  5. 15

15.6 References

  1. The Comprehensive R Archive NetworkRcran: Url: https://cran.r-project.org/
  2. RStudio official website. Url: https://rstudio.com/
  3. Anaconda official website. Url: https://www.anaconda.com/
  4. Introduction to R. Datacamp interactive course. Url: https://www.datacamp.com/courses/free-introduction-to-r
  5. Quanargo. Introduction to R. Url: https://www.quantargo.com/courses/course-r-introduction
  6. R Coder Project. Begin your data science career with R language! Url: https://r-coder.com/
  7. R Core Team (2019). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria.URL https://www.R-project.org/.
  8. A.B. Shipunov, EM Baldin, P.A. Volkova, VG Sufiyanov. Visual statistics. We use R! - M .: DMK Press, 2012. - 298 p .: ill.
  9. An Introduction to R. URL: https://cran.r-project.org/doc/manuals/r-release/R-intro.html
  10. R programming. https://www.datamentor.io/r-programming
  11. Learn R. R Functions. https://www.w3schools.com/r/r_functions.asp
  12. UC Business Analytics R Programming Guide. Managing Data Frames. http://uc-r.github.io/dataframes
  13. Learn R programming. R - Lists. https://www.tutorialspoint.com/r/r_lists.htm
  14. Tutorial on the R Apply Family by Carlo Fanara. https://www.datacamp.com/community/tutorials/r-tutorial-apply-family