2017-08-15 102 views
1
> 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

最有可能的罪魁禍首是,你是用因子變量工作,必須使用'as.character'要挾變量之前使用'strsplit'。 – lmo

+0

你可以做一個base_mat dput嗎? – Mark

+1

'x < - 「CTGCAGAGGCTATTTCCTGGAATGAATCAACGAGTGAAACGAATAAC」; 012rsxlit(x,「」)'在'strsplit()''split ='中使用空字符串 – jogo

回答

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" 
相關問題