Match vectors in sequence : <p>I have 2 vectors. </p>

<pre><code>x=c("a", "b", "c", "d", "a", "b", "c")
y=structure(c(1, 2, 3, 4, 5, 6, 7, 8), .Names = c("a", "e", "b", 
"c", "d", "a", "b", "c"))
</code></pre>

<p>I would like to match <code>a</code> to <code>a</code>, <code>b</code> to <code>b</code> in sequence accordingly, so that <code>x[2]</code> matches <code>y[3]</code> rather than <code>y[7]</code>; and <code>x[5]</code> matches <code>y[6]</code> rather than <code>y[1]</code>, so on and so forth. </p>

<pre><code>lapply(x, function(z) grep(z, names(y), fixed=T))
</code></pre>

<p>gives: </p>

<pre><code>[[1]]
[1] 1 6

[[2]]
[1] 3 7

[[3]]
[1] 4 8

[[4]]
[1] 5

[[5]]
[1] 1 6

[[6]]
[1] 3 7

[[7]]
[1] 4 8
</code></pre>

<p>which matches all instances. How do I get this sequence: </p>

<pre><code>1 3 4 5 6 7 8
</code></pre>

<p>So that elements in <code>x</code> can be mapped to the corresponding values in <code>y</code> accordingly? </p>
