I have a data frame with three strings, mature
, star
and precursor
.
The columns mature
and star
is a substring of precursor
. I would like to add a new column in the data frame that says if the mature string is matching the left or the right part of the precursor string.
In my example, the first row is matching the left part of its precursor and the second row is matching the right part of its precursor. Left and right should be defined as from the middle of the precursor string. However, the substring is not always at the very beginning or the very end of the precursor, somethings it starts as position 2 or 3.
Is there a way of doing this using stringr, or any other R package?
df <- structure(list(mature = c("uggagugugacaaugguguuu", "cuauacaacuuacugucuuucc"
), star = c("aacgccauuaucacacuaaau", "ugagguaguagguuguauag"
), precursor = c("uggagugugacaaugguguuuguguccuccguaucaaacgccauuaucacacuaaau",
"ugagguaguagguuguauaguuuuagggucauucccaagcugucagaugacuauacaacuuacugucuuucc"
)), row.names = 1:2, class = "data.frame")
I looked at str_locate_all which gives me the position of the mature relative to the precursor.
> str_locate_all(pattern =df$mature, df$precursor)
[[1]]
start end
[1,] 1 21
[[2]]
start end
[1,] 51 72
question from:
https://stackoverflow.com/questions/65917094/define-if-substring-is-matching-left-or-right-part-of-original-string 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…