Finding string position (index) in another string for each row of a data.frame

Question

In my data.frame, I have two columns with text (say, str1 and str2). I want to add a column that indicates the position of str2 in str1 for each row.

An example for the data.frame:

dt <- data.frame(str1 = c('ab/some words,cd/some words', 
'cd/some words,ab/some words', 'ab/some words,cd/some words', 'ef/some 
words,ab/some words'), str2 = c('ab', 'cd', 'cd', 'ef'))

I want to add a column that indicates the position of str2 in str1 for each row.

This doesn't work:

dt$str2.pos <- regexpr(dt$str2, dt$str1)

I'm trying to get an output that looks like this:

                         str1 str2 str2.pos
1 ab/some words,cd/some words   ab        1
2 cd/some words,ab/some words   cd        1
3 ab/some words,cd/some words   cd       15
4 ef/some words,ab/some words   ef        1

Show source
| R   | string   2017-01-07 10:01 1 Answers

Answers to Finding string position (index) in another string for each row of a data.frame ( 1 )

  1. 2017-01-07 10:01

    We can do

    dt$str2.pos <- diag(sapply(dt$str2, function(x) regexpr(x, dt$str1)))
    dt$str2.pos
    #[1]  1  1 15  1
    

    If we are doing this for corresponding rows, then use mapply/Map

    mapply(regexpr, dt$str2, dt$str1)
    #[1]  1  1 15  1
    

Leave a reply to - Finding string position (index) in another string for each row of a data.frame

◀ Go back