Counting the frequency of differing patterns in a character string

  • A+

I currently have a string in R that looks like this:


First, I need to determine the frequency of different patterns of "M" that appear in the string.

In this example it would be:

MM = 2 MMM = 2 MMMM = 1 

Secondly, I then need to designate a numerical value/score for each different pattern.


MM = 1 MMM = 2 MMMM = 3 

This would mean that the total value/score of M's in a would equal 9.

If anyone knows any script that would allow me to do this for multiple strings like this in a dataframe that would be great?

Thank you.


a <- "BMMBMMMMBMMMBMMBBMMM"     tbl <- table(strsplit(a, "B"), exclude="") tbl #     MM  MMM MMMM  #      2    2    1   score <- sum(tbl * 1:3) score # 9 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: