2

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))

}
zx8754
  • 52,746
  • 12
  • 114
  • 209
Corend
  • 343
  • 1
  • 3
  • 18

2 Answers2

7

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), "")
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Do note that the `strsplit` itself returns a list object, with an entry for each element in the input vector (`unlist(Seq.genes)`). The code you provided will give the exact output as OP requested. – MrGumble Nov 02 '17 at 10:02
  • @MrGumble Yes, it returns a list object. In this case it may be better to have a list – akrun Nov 02 '17 at 10:05
-1
sapply(Seq.genes, strsplit, split = '')

or

lapply(Seq.genes, strsplit, split = '')
pzhao
  • 335
  • 1
  • 10