Reputation: 6302
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
.
Upvotes: 4
Views: 366
Reputation: 39667
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
Upvotes: 2
Reputation: 21400
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
Upvotes: 2
Reputation: 351
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])
Upvotes: 0