- 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:

推薦閱讀
- Microsoft Dynamics CRM Customization Essentials
- 大數據戰爭:人工智能時代不能不說的事
- 構建高質量的C#代碼
- Visual Basic從初學到精通
- PostgreSQL 10 Administration Cookbook
- Python:Data Analytics and Visualization
- PVCBOT機器人控制技術入門
- 從零開始學Java Web開發
- Mastering Ceph
- 基于人工免疫原理的檢測系統模型及其應用
- RealFlow流體制作經典實例解析
- Hands-On Microservices with C#
- EDA技術及其創新實踐(Verilog HDL版)
- 中老年人學數碼照片后期處理
- 單片機C語言編程實踐