-4

I have a large sequence of strings containing only the following characters

"M", "D", "A"

such as:

 "M" "M" "A" "A" "D" "D" "M" "D" "A"

and I would like to compress it to:

M2A2D2M1D1A1 

in R. Googling has led me to this (a java solution) but before implementing it, it would be interesting to check if I can find something ready online. Thanks!

elixir
  • 752
  • 1
  • 7
  • 25
Kots
  • 486
  • 1
  • 5
  • 21

1 Answers1

3

R function rle() is your friend.

testVector <- sample(c("M", "D", "A"), 20, replace=T)
res <- rle(testVector)
compressedString <- paste(res$values, res$lengths, collapse = "", sep = "")
Kots
  • 486
  • 1
  • 5
  • 21