1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 |
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:
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
12 | 14 | 15 |
22 | 15 | 21 |
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
c1 | c2 | c3 | |
---|---|---|---|
row1 | 1 | 4 | 7 |
row2 | 2 | 5 | 8 |
row3 | 3 | 6 | 9 |
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 | B |
---|---|
1 | 101 |
2 | 102 |
3 | 103 |
C | D |
---|---|
201 | 1001 |
202 | 1002 |
203 | 1003 |
A | B | C | D |
---|---|---|---|
1 | 101 | 201 | 1001 |
2 | 102 | 202 | 1002 |
3 | 103 | 203 | 1003 |
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:
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):
To display the same element using row and column indexes, write as follows:
Question: What record should you use ti get 18?
Answer: m[2,3]
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:
- 11
- 14
- 17
- 20
- 23
11 | 14 | 17 | 20 | 23 |
13 | 16 | 19 | 22 | 25 |
- 11
- 12
- 13
11 | 17 |
12 | 18 |
13 | 19 |
You can also specify a list of rows and columns to be output / received simultaneously:
You can exclude individual columns or rows by using indexes with minus signs (-
):
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 ")))
1 | 6 | 11 |
2 | 7 | 12 |
3 | 8 | 13 |
4 | 9 | 14 |
5 | 10 | 15 |
[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:
15.6 References
- The Comprehensive R Archive NetworkRcran: Url: https://cran.r-project.org/
- RStudio official website. Url: https://rstudio.com/
- Anaconda official website. Url: https://www.anaconda.com/
- Introduction to R. Datacamp interactive course. Url: https://www.datacamp.com/courses/free-introduction-to-r
- Quanargo. Introduction to R. Url: https://www.quantargo.com/courses/course-r-introduction
- R Coder Project. Begin your data science career with R language! Url: https://r-coder.com/
- 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/.
- A.B. Shipunov, EM Baldin, P.A. Volkova, VG Sufiyanov. Visual statistics. We use R! - M .: DMK Press, 2012. - 298 p .: ill.
- An Introduction to R. URL: https://cran.r-project.org/doc/manuals/r-release/R-intro.html
- R programming. https://www.datamentor.io/r-programming
- Learn R. R Functions. https://www.w3schools.com/r/r_functions.asp
- UC Business Analytics R Programming Guide. Managing Data Frames. http://uc-r.github.io/dataframes
- Learn R programming. R - Lists. https://www.tutorialspoint.com/r/r_lists.htm
- Tutorial on the R Apply Family by Carlo Fanara. https://www.datacamp.com/community/tutorials/r-tutorial-apply-family