Reputation: 21
I have x = c("AU|30|3020","AU|15|1510","AU|2000|510")
.
If I want to know the position of the last “|” for each element in x, how do I do it. The answer should be 6 6 8.
An acceptable alternative is to find the position of (say) the second "|" for each element (as opposed to last "|").
Upvotes: 2
Views: 132
Reputation: 81683
You can get the position of the last |
(independent of the total number of |
s) with the following command:
unlist(gregexpr("\\|[^|]*$", x))
# [1] 6 6 8
Upvotes: 2
Reputation: 3711
are there always 2 |
? if so
unlist(lapply(gregexpr("\\|",x),"[[",2))
otherwise you may need workaround. Also, look at stringr
package
Upvotes: 0