0

Suppose I have a character vector:

test <- c("a##", "b", "c##", "d", "e")

I want to retrieve the indexes of elements ending with "##".

How can I do that?

Frank
  • 66,179
  • 8
  • 96
  • 180
Ben
  • 6,321
  • 9
  • 40
  • 76

1 Answers1

10

Here is one method using grep:

grep("##$", test)

This returns indices 1 and 3 as a vector. The "##$" is a regular expression that says match if ## are the last two characters, the "$". anchors the ## to the end.

lmo
  • 37,904
  • 9
  • 56
  • 69