1

I have two vectors with the same elements but in different order, how can I find the new position of the elements ? which() should do the trick but I'm unable to figure out how to do that, any idea ?

For example :

v1 <- c("paris", "new york", "london", "sidney")
v2 <- c("sidney", "new york", "paris", "london")

> which(v1 %in% v2)
[1] 1 2 3 4
> which(v1 == v2)
integer(0)

What I'm trying to do is something like :

> find_new_position(v1, v2)
[1] 3 2 4 1
Florent
  • 1,791
  • 22
  • 40

1 Answers1

3

use match

match(v1, v2)

Returns the indices for matching elements, NA if element is not in the list to match against.

Michael Andorfer
  • 1,660
  • 5
  • 25
  • 45