Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add values to a specific matrix row-column

Tags:

r

matrix

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?

like image 710
repinementer Avatar asked Apr 06 '11 08:04

repinementer


People also ask

How do you append a row in a matrix?

Adding Row To A Matrix We use function rbind() to add the row to any existing matrix.

How do you add values to a matrix 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.

Can we change rows and columns in matrix?

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.


1 Answers

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

like image 123
csgillespie Avatar answered Nov 15 '22 08:11

csgillespie