Corend
Corend

Reputation: 343

List of string to list of vectors of characters

After defining

> Seq.genes <- as.list(c("ATGCCCAAATTTGATTT","AGAGTTCCCACCAACG"))

I have a list of strings :

> Seq.genes[1:2]
[[1]]
[1] "ATGCCCAAATTTGATTT"

[[2]]
[1] "AGAGTTCCCACCAACG"

I would like to convert it in a list of vectors :

>Seq.genes[1:2]
[[1]]
[1]"A" "T" "G" "C" "C" "C" "A" "A" "A" "T" "T" "T" "G" "A" "T" "T" "T"

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

I tried something like :

for (i in length(Seq.genes)){
  x <- Seq.genes[i]
  Seq.genes[i] <- substring(x, seq(1,nchar(x),2), seq(1,nchar(x),2))

}

Upvotes: 2

Views: 89

Answers (2)

pzhao
pzhao

Reputation: 335

sapply(Seq.genes, strsplit, split = '')

or

lapply(Seq.genes, strsplit, split = '')

Upvotes: -1

akrun
akrun

Reputation: 886938

It may be better to have the strings in a vector rather than in a list. So, we could unlist, then do an strsplit

strsplit(unlist(Seq.genes), "")

Upvotes: 7

Related Questions