Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12141

How to combine two vectors by a common element?

$
0
0

Suppose I have

vec1 = c(1,2)vec2 = c(2,3)

How do you combine to achieve output vector 1 2 3 or 3 2 1?

Similarly

vec1 = c(3,1)vec2 = c(3,2)

Desired output is 1 3 2 or 2 3 1.

For the life of me I cannot work this out.

I've tried intersect and union combinations but these both seem to sort the sets on output, I'm looking for an unsorted output as per example 2 above. Is there a left and right join option in R that joins on common elements? I'm trying to achieve a general function that I can pass two vectors (of any lengths with a common element) that will combine the two as per above.


Viewing all articles
Browse latest Browse all 12141

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>