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

From: Ted Harding <Ted.Harding_at_manchester.ac.uk>
Date: Mon, 02 Jun 2008 10:15:52 +0100 (BST)


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 ------------------------------

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 02 Jun 2008 - 09:19:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 02 Jun 2008 - 09:30:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive