- R Programming Fundamentals
- Kaelen Medeiros
- 199字
- 2021-07-23 16:58:21
Matrices
A matrix is a 2D vector with rows and columns. In R, one requirement for matrices is that every data element stored inside it be of the same type (all character, all numeric, and so on). This allows you to perform arithmetic operations with matrices, if, for example, you have two that are both numeric.
Let's use matrix() to create a matrix, examine its class, use rownames() and colnames() to set row and column names, and access different elements of the matrix using multiple methods. Follow the steps given below:
- Use matrix() to create matrix1, a 3 x 3 matrix containing the numbers 1:12 by column, using the following code:
matrix1 <- matrix(c(1:12), nrow = 3, ncol = 3, byrow = FALSE)
- Create matrix2 similarly, also 3 x 3, and fill it with 1:12 by row, using the following code:
matrix2 <- matrix(c(1:12), nrow = 3, ncol = 3, byrow = TRUE)
- Set the row and column names of matrix1 with the following:
rownames(matrix1) <- c("one", "two", "three")
colnames(matrix1) <- c("one", "two", "three")
- Find the elements at the following positions in matrix1 using matrix indexing:
matrix1[1, 2]
matrix1["one",]
matrix1[,"one"]
matrix1["one","one"]
The output of the code is as follows:

推薦閱讀
- 課課通計(jì)算機(jī)原理
- Word 2000、Excel 2000、PowerPoint 2000上機(jī)指導(dǎo)與練習(xí)
- B2B2C網(wǎng)上商城開(kāi)發(fā)指南
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)
- 液壓機(jī)智能故障診斷方法集成技術(shù)
- Mastering Ceph
- FANUC工業(yè)機(jī)器人配置與編程技術(shù)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)實(shí)訓(xùn)(職業(yè)模塊)
- 步步驚“芯”
- 中文版Photoshop情境實(shí)訓(xùn)教程
- 網(wǎng)絡(luò)安全原理與應(yīng)用
- ROS Robotics By Example(Second Edition)
- Flash 8中文版全程自學(xué)手冊(cè)
- 軟件需求最佳實(shí)踐
- 單片機(jī)原理、接口及應(yīng)用系統(tǒng)設(shè)計(jì)