4

Given the following two character vectors

stringA = c("AA", "BB", "CC", "BB", "DD", "CC")
stringB = c("BB", "CC")

I want to find the positions of stringB within stringA.

The result should be a vector of length stringB, hence for this example: c(2, 3). A vector of length one only containing the start position of the sequence (here: 2) would also be ok (since I know the length of stringB and could operate further with it).

It can be assumed that the sequence of stringB is unique within stringA.

pat-s
  • 5,992
  • 1
  • 32
  • 60

3 Answers3

2

You're after positions of elements in vectors. Positions are equivalent to indices.

To grab TRUE indices (that is, those indices the elements of which evaluate to TRUE on some condition--in your case, that the unique elements of stringA be contained in stringB), you can use which:

which(unique(stringA) %in% stringB)
[1] 2 3
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
2

In case you are looking for the position where the whole sequence of stringB starts in stringA you can use:

which(rowSums(!sapply(seq(stringB), function(i) stringB[i] ==
   stringA[i:(length(stringA)-length(stringB)+i)])) == 0)
#[1] 2
GKi
  • 37,245
  • 2
  • 26
  • 48
  • I ended up using this answer since it is the only one that worked for my practical problem. – pat-s May 14 '20 at 19:57
0

if you're looking every position of stringB in stringA, then you could use this:
lapply(stringB, function(x) which(x == stringA))

alternatively, if you just want a vector containing the first position of stringB in stringA, you could try this:
sapply(stringB, function(x) which(x == stringA)[1])

CourtesyBus
  • 331
  • 2
  • 4