I currently have a string in R that looks like this:
a <- "BMMBMMMMBMMMBMMBBMMM"
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
If anyone knows any script that would allow me to do this for multiple strings like this in a dataframe that would be great?
a <- "BMMBMMMMBMMMBMMBBMMM" tbl <- table(strsplit(a, "B"), exclude="") tbl # MM MMM MMMM # 2 2 1 score <- sum(tbl * 1:3) score # 9