r - Removing repeated letters/alphabets -
input : kdff455556tfkkkw
output1 : kdf[2]45[4]6tfk[3]w
hint: repeated "alphnum" alphnum[no_of_repetition]
s <- c('kdff455556tfkkkw','abc','abbccc'); sapply(strsplit(s,''),function(x) paste(with(rle(x),ifelse(lengths==1,values,paste0(values,'[',lengths,']'))),collapse='')); ## [1] "kdf[2]45[4]6tfk[3]w" "abc" "ab[2]c[3]"
Comments
Post a Comment