Position of pattern matches in R dataframe column

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

Count the number of pattern matches in R dataframe column 1

 

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

Count the number of pattern matches in R dataframe column 2

 

                                                                                                     

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts