To get the Position of pattern matches in R we use str_locate() function of “stringr” package. Let’s see how to get the position of pattern matches in R with an example.
Let’s first create the dataframe.
df1 = data.frame(Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), Mathematics_score=c(45,78,44,89,66,NaN,NaN)) df1
so the dataframe will be
Get the position of pattern matches in R:
str_locate() function is used to get the position of pattern matches in R
library(stringr) df1$A_position <- str_locate(df1$Name,"a") df1
Start and end position of “a” in the column “Name” is shown. So the resultant dataframe will be