-1

seq="GAGTAGGAGGAG",how to split this sequence into the following sub sequence "GAG","TAG","GAG","GAG"i.e how to split the sequence in groups of threes

1 Answers1

1

We can create a function called fixed_split that will split a character string into equal parts. The regular expression is a lookbehind that matches on n elements together:

fixed_split <- function(text, n) {
  strsplit(text, paste0("(?<=.{",n,"})"), perl=TRUE)
}

fixed_split("GAGTAGGAGGAG", 3)
[[1]]
[1] "GAG" "TAG" "GAG" "GAG"

Edit

In your comment you say sequence ="ATGATGATG" does not work:

strsplit(sequence,"(?<=.{3})", perl=TRUE)
[[1]]
[1] "ATG" "ATG" "ATG"
Pierre L
  • 26,748
  • 5
  • 39
  • 59