- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add a value to a particular matrix element in R?
To add a value to a particular matrix element in R, we can use subsetting for the particular value with single square brackets.
For Example, if we have a matrix called M and we want to add 10 to the fifth value in second column then can use the below mentioned command −
M[5,2]<-M[5,2]+10
Example 1
Following snippet creates a sample matrix −
M1<-matrix(sample(1:100,80),ncol=4) M1
The following matrix is created −
[,1] [,2] [,3] [,4] [1,] 29 72 16 75 [2,] 5 79 15 4 [3,] 81 8 46 83 [4,] 97 50 18 32 [5,] 62 96 64 82 [6,] 27 92 9 22 [7,] 69 37 70 28 [8,] 20 58 14 66 [9,] 13 45 19 52 [10,] 11 42 34 80 [11,] 61 99 86 55 [12,] 63 3 78 36 [13,] 53 33 1 40 [14,] 24 77 76 2 [15,] 54 47 48 71 [16,] 87 68 88 38 [17,] 67 12 44 26 [18,] 17 7 91 10 [19,] 49 90 30 59 [20,] 74 51 31 89
To add 1000 to first value in the matrix M1 on the above created data frame, add the following code to the above snippet −
M1<-matrix(sample(1:100,80),ncol=4) M1[1,1]<-M1[1,1]+1000 M1
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[,1] [,2] [,3] [,4] [1,] 1029 72 16 75 [2,] 5 79 15 4 [3,] 81 8 46 83 [4,] 97 50 18 32 [5,] 62 96 64 82 [6,] 27 92 9 22 [7,] 69 37 70 28 [8,] 20 58 14 66 [9,] 13 45 19 52 [10,] 11 42 34 80 [11,] 61 99 86 55 [12,] 63 3 78 36 [13,] 53 33 1 40 [14,] 24 77 76 2 [15,] 54 47 48 71 [16,] 87 68 88 38 [17,] 67 12 44 26 [18,] 17 7 91 10 [19,] 49 90 30 59 [20,] 74 51 31 89
Example 2
Following snippet creates a sample matrix −
M2<-matrix(rnorm(40),ncol=2) M2
The following matrix is created −
[,1] [,2] [1,] 0.7053897 -1.9200364 [2,] -1.1234320 1.6390770 [3,] -0.1418606 2.2555713 [4,] 0.9207447 -1.5381620 [5,] -0.2770107 0.1623484 [6,] -0.8251034 -1.3255143 [7,] -0.4287154 -2.3155433 [8,] -0.9181893 0.7627583 [9,] -0.0384247 -0.6836569 [10,] 0.5226378 -0.8815467 [11,] -0.8472512 -0.4861481 [12,] 1.0694954 1.5913287 [13,] 0.6082448 -0.6050546 [14,] -0.3389231 -0.1138805 [15,] -1.6208191 -1.2074059 [16,] 0.5841005 -0.1632070 [17,] 1.4022579 -2.8237466 [18,] -1.3758415 -0.3331492 [19,] -1.0062265 0.2886416 [20,] 2.1965559 0.8964789
To add 5 to first value in second column of matrix M2 on the above created data frame, add the following code to the above snippet −
M2<-matrix(rnorm(40),ncol=2) M2[1,2]<-M2[1,2]+5 M2
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[,1] [,2] [1,] 0.7053897 3.0799636 [2,] -1.1234320 1.6390770 [3,] -0.1418606 2.2555713 [4,] 0.9207447 -1.5381620 [5,] -0.2770107 0.1623484 [6,] -0.8251034 -1.3255143 [7,] -0.4287154 -2.3155433 [8,] -0.9181893 0.7627583 [9,] -0.0384247 -0.6836569 [10,] 0.5226378 -0.8815467 [11,] -0.8472512 -0.4861481 [12,] 1.0694954 1.5913287 [13,] 0.6082448 -0.6050546 [14,] -0.3389231 -0.1138805 [15,] -1.6208191 -1.2074059 [16,] 0.5841005 -0.1632070 [17,] 1.4022579 -2.8237466 [18,] -1.3758415 -0.3331492 [19,] -1.0062265 0.2886416 [20,] 2.1965559 0.8964789
- Related Articles
- How to check which list element contains a particular value in R?
- How to add a new value to each element of list in R?
- How to delete matrix rows if a particular column value satisfies some condition in R?
- How to add a new column to a matrix in R?
- How to remove a particular value from a vector in R?
- How to subset a matrix based on values in a particular column in R?
- How to replace a particular value in R data frame with a new value?
- How to add a vector to each row of a matrix in R?
- How to add or multiply each element of a matrix to the corresponding element of another matrix in R, if these matrices are stored as a list?
- How to check in R whether a matrix element is present in another matrix or not?
- How to convert a matrix to binary matrix in R?
- How to find the coordinate of a value in an R matrix?
- How to check if a matrix has any missing value in R?
- How to convert a matrix into a color matrix in R?
- How to convert a sparse matrix into a matrix in R?
