Almost the same as @db, but converted to a couple of functions, so it's reusable and easy to read:
abbreviate_letters <- function(type_letters) { runs <- rle(type_letters) run_codes <- ifelse( runs[["lengths"]] == 1, yes = runs[["values"]], no = paste0("(", runs[["values"]], runs[["lengths"]], ")") ) paste0(run_codes, collapse = "") } condense_haplotype <- function(haplotype_long) { split_terms <- strsplit(haplotype_long, split = "") vapply( X = split_terms, FUN = abbreviate_letters, FUN.VALUE = character(1) ) } haplotypes <- c( "SKNNNRNNNNNKNNNNNNNKF", "SKNNNNNNNNNKNNNNNNNNKF", "SKNNNNNNNNNNNNNNNNKF" ) condense_haplotype(haplotypes)
Nathan werth
source share