字符向量列表的字符串列表

时间:2017-11-02 09:57:58

标签: r

定义后

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

我有一个字符串列表:

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

[[2]]
[1] "AGAGTTCCCACCAACG"

我想将它转换为矢量列表:

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

我尝试过类似的事情:

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

}

2 个答案:

答案 0 :(得分:7)

将字符串放在vector而不是list中可能更好。因此,我们可以unlist,然后执行strsplit

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

答案 1 :(得分:-1)

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

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