[R] request: To add an extra row in a matrix

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Jun 2 11:15:52 CEST 2008


On 02-Jun-08 09:03:38, Muhammad Azam wrote:
> Dear R users
> I have a problem regarding an addition of an extra "row" to a matrix.
> e.g. i have a matrix
> a <- matrix(1:6,2,3)
>> a
>      [,1] [,2] [,3]
> [1,]    1    3    5
> [2,]    2    4    6
> I want to add a matrix having just one row. e.g.
> b <- matrix(7:9,1,3)
> 
>> b
>      [,1] [,2] [,3]
> [1,]    7    8    9
> Now i want to get result like this
>      [,1] [,2] [,3]
> [1,]    1    3    5
> [2,]    2    4    6
> [3,]    7    8    9
> Can any body help to get the required result.  Thanks and
> best regards
> Muhammad Azam

The simplest way is to use rbind():

  a <- matrix(1:6,2,3)
  a
#     [,1] [,2] [,3]
#[1,]    1    3    5
#[2,]    2    4    6
  b <- matrix(7:9,1,3)
  b
#     [,1] [,2] [,3]
#[1,]    7    8    9
  rbind(a,b)
#     [,1] [,2] [,3]
#[1,]    1    3    5
#[2,]    2    4    6
#[3,]    7    8    9

You can use rbind() in the same way to augment 'a' by 'b' when
'b' consists of more than 1 row. Similarly, cbind() would augment
'a' by adjoining extra columns.

Look at
  ?cbind
for information about both rbind() and cbind().

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 02-Jun-08                                       Time: 10:15:48
------------------------------ XFMail ------------------------------



More information about the R-help mailing list