I have the following matrix:
1 2 3 4
2 3 4 5
and I want to add 10 to 3rd column 2nd row:
1 2 3 4
2 3 14 5
How should I do it?
Adding Row To A Matrix We use function rbind() to add the row to any existing matrix.
To add a value to a particular matrix element in R, we can use subsetting for the particular value with single square brackets.
Can we interchange rows in a matrix? Yes, we can interchange the rows of a matrix to get a new matrix. For example, R1↔R2 or R1↔R3 and so on.
If m
is your matrix, then
> m = matrix(0, 2, 4)
> m[2,3] = m[2,3] + 10
> m
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 10 0
Any book in R will have details on how to access specific elements. In the meantime have a look at Chapter 5 of An Introduction to R
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With