如何将字符串向量转换为字符数组

时间:2017-08-15 12:33:57

标签: r string

> head(base_mat)
                                                                                                     V1
1 CTGCAGAGGCTATTTCCTGGAATGAATCAACGAGTGAAACGAATAACTCTATGGTGACTGAATTCATTTTTCTGGGTCTCTCTGATTCTCAGGAACTCCAG
2 TCTGCTGTGAGGCAGACGTTGGGACTGTAGAGGCTGACGGGAGGCAGAGGCTGGGCCTGGAGGGGCCACCAAGATGCAGGAGCTGGGCCTGGAGAGGCTGC
3 CATAATCACAATTTCCTCCCCATTTTCTTAGTTCTCATAATTTTAGCCACAGCCCAGTTGGCTGGACCAATGGATGGAGAGAATCACTCAGTGGTATCTGA
4 TCAGATACCACTGAGTGATTCTCTCCATCCATTGGTCCAGCCAACTGGGCTGTGGCTAAAATTATGAGAACTAAGAAAATGGGGAGGAAATTGTGATTATG
5 CCAAATACCTGCACAGTATGTATGATAAATGCATATGATAAAGTAAAAAAAAAAATAGCACACACTGAAAGAAAGCCAACAGAAGAGGGCACTGGGCATGG
6 TTGAGCTGGGATTATAGTTGACATTTCATTATAATATATTAGCTATTCTAGACTTTATGCATTTATGTAAAGTTTTCTTTGTTGCACTTTAAGTTCTGTGA

我想将其转换为字符数组,但strsplit()函数不起作用。

> strsplit(head(base_mat),split="")
Error in strsplit(head(base_mat), split = "") : 非字符参数

我希望字符串的每一列都是data.frame的变量。 比如

V1    V2    V3
C     T     G   ...
T     C     T
...

怎么做?

1 个答案:

答案 0 :(得分:1)

感谢@Imo提醒我factor变量问题和@jogo解决方案。我的问题可以解决如下。

> options(stringsAsFactors = FALSE)
> x <- c("CTGCAGAGGCTATTTCCTGGAATGAATCAACGAGTGAAACGAATAAC","CTGCAGAGGCTATTTCCTGGAATGAATCAACGAGTGAAACGAATAAC")
> x <- data.frame(x)
> strsplit(x, "")
Error in strsplit(x, "") : non-character argument
> strsplit(x[,1], "")
[[1]]
 [1] "C" "T" "G" "C" "A" "G" "A" "G" "G" "C" "T" "A"
[13] "T" "T" "T" "C" "C" "T" "G" "G" "A" "A" "T" "G"
[25] "A" "A" "T" "C" "A" "A" "C" "G" "A" "G" "T" "G"
[37] "A" "A" "A" "C" "G" "A" "A" "T" "A" "A" "C"

[[2]]
 [1] "C" "T" "G" "C" "A" "G" "A" "G" "G" "C" "T" "A"
[13] "T" "T" "T" "C" "C" "T" "G" "G" "A" "A" "T" "G"
[25] "A" "A" "T" "C" "A" "A" "C" "G" "A" "G" "T" "G"
[37] "A" "A" "A" "C" "G" "A" "A" "T" "A" "A" "C"