# [R] Combine vectors under the names

Boris Steipe boris.steipe at utoronto.ca
Mon Apr 3 03:45:56 CEST 2017

```Your code is syntactically correct but goes against all R style guides I know. I've changed that - but obviously you don't have to.

x1 <- c(a1 = 0, b3 = 2, e2 = -2)
x2 <- c(c = 3, d = 4, f = 5)
N <- c("a1", "b3", "d", "e2", "c", "f")

x3 <- c(x1, x2)   # concatenate
x3 <- x3[N]       # re-order

The assumption is that N contains each of names(x1) and names(x2) exactly once.
If that isn't guaranteed, a different approach is needed.

B.

> On Apr 2, 2017, at 5:02 PM, Art U <art.tem.us at gmail.com> wrote:
>
> Hello,
>
> Lets say I have 2 vectors:
> x1=c("a1"=0,"b3"=2,"e2"=-2);
> x2=c("c"=3,"d"=4,"f"=5);
> and vector of names in specific order:
> N=c("a1","b3","d","e2","c","f")
> and I want to combine them to vector C:
>
> C=
>
> a1 b3  d e2  c  f
> 0  2  4 -2  3  5
>
>
> Basically, just fill vector N with values from vector x1 and x2. How can I
> do that?
> Art
>
> --
> *I like to pretend I'm alone*. *Completely alone*. *Maybe post-apocalypse
> or plague*... *Whatever*. *No-one left to act normal for. No need to hide
> who I really am. It would be... freeing*. *...*
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help