1

Say I have two character vectors

vec <- c('A', 'B', 'C', 'D', 'E') 
pat <- c('D', 'B', 'A')

how do I get the indexes of the occurrences in vec of the values in pat in the order they appear in pat?

I can try

which(vec %in% pat)

but this gives me them in the incorrect order: 1 2 4. I want them as 4 2 1.

3
  • 9
    Try match(pat, vec) Commented Jul 24, 2016 at 7:02
  • Searching for the man for help("%in%") returns the man for match. Commented Jul 24, 2016 at 7:17
  • %in% is also documented on that help page. Commented Jul 24, 2016 at 9:50

1 Answer 1

1

I tried different ways to solve this problem before and always found that the easiest way to solve it is the solution as mentioned in @DavidArenburg's comment:

match(pat, vec)
# [1] 4 2 1
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.