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

Updated on: 10-Nov-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements