Search code examples
rstringregex-group

How to paste characters in groups of 3 in R


Here is a DNA string that I want to split and then combine in groups of 3

dna=c("TACACGATGACAGTCTTGACGGGTTCTCCTACT")
dna.sg = unlist(strsplit(dna, ""))

Gives

 [1] "T" "A" "C" "A" "C" "G" "A" "T" "G" "A" "C" "A" "G" "T" "C" "T" "T" "G" "A" "C" "G" "G" "G" "T" "T" "C" "T" "C" "C" "T" "A" "C" "T"

But I'd like to have

"TAC" "ACG" [...]

Solution

  • You may split every 3 characters in strsplit.

    unlist(strsplit(dna, "(?<=.{3})", perl = TRUE))
    #[1] "TAC" "ACG" "ATG" "ACA" "GTC" "TTG" "ACG" "GGT" "TCT" "CCT" "ACT"