Search code examples
rmatrixvectorfasta

splitting a vector of letters into vectors of equal size


I have some fasta sequence which is made out of hundreds of thousand of letters "a" "g" "c" "t"

library(seqinr)
mydata<-read.fasta(file="sequence.fasta")
mydata1<-mydata[[1]]

I would like cut it into vectors of equal lengths of let's say size 100. Manually it would look something like

vec1<-mydata1[c(1:100)]
vec2<-mydata1[c(101:200)]

etc. I do not have access to any other coding program hence it must be done in r. I am thinking about a for loop of some kind but I don't know how to implement one. Is this task even possible in r?

EDIT: This question is not the same as the one highlighted because I don't have a numeric vector but DNA sequence made out of letters.


Solution

  • If we need to split by vectors of length 100

    lst <- split(my.data, as.integer(gl(length(my.data), 100, length(my.data))))