# Re: [R] Simple... but...

From: Ted Harding <Ted.Harding_at_manchester.ac.uk>
Date: Wed, 23 Jul 2008 14:30:34 +0100 (BST)

On 23-Jul-08 12:54:49, Shubha Vishwanath Karanth wrote:
> Hi R,
> If
> x=c(1,3,5)
> y=c(2,4,6)
>
> I need a vector which is c(1,2,3,4,5,6) from x and y.
> How do I do it? I mean the best way....
> Thanks, Shubha

Your query is ambiguous, in that it is not clear whether you want a) The elements of the combination of x and y to be in increasing

order, regardless of how they are distributed between x and y

In which case a simple solution is sort(c(x,y))

b) The elements to be taken alternately from x and y, regardless

of their values

In which case a simple solution is as.vector(rbind(x,y))

Example:

x <- c(1.1,1.2,1.3)
y <- c(2.1,2.2,2.3)

## (a):
sort(c(x,y))
# [1] 1.1 1.2 1.3 2.1 2.2 2.3

## (b)
as.vector(rbind(x,y))
# [1] 1.1 2.1 1.2 2.2 1.3 2.3

The key to solution (b) is that R reads a matrix down the columns, and rbind(x,y) puts x as a row above y as a row, so reading down the columns alternates between x and y.

Hoping this helps,
Ted.

E-Mail: (Ted Harding) <Ted.Harding_at_manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861
```Date: 23-Jul-08                                       Time: 14:30:31
------------------------------ 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 Wed 23 Jul 2008 - 15:12:57 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 Wed 23 Jul 2008 - 15:32:16 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.