其實我有同樣的問題,這種情況下strsplit one column with exact information into two column拆分一列R中兩列循環
這個問題已經解決了,只是我的數據看起來就像
SNP Geno AlleleA AlleleB AlleleC AlleleD AlleleE
1 marker1 G1 AA AA AA AA AA
2 marker2 G1 TT TT TT TT TT
3 marker3 G1 TT TT TT TT TT
4 marker1 G2 CC CC CC CC CC
5 marker2 G2 AA AA AA AA AA
6 marker3 G2 TT TT TT TT TT
7 marker1 G3 GG GG GG GG GG
8 marker2 G3 AA AA AA AA AA
9 marker3 G3 TT TT TT TT TT
dput輸出:
structure(list(SNP = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L), .Label = c("marker1", "marker2", "marker3"), class = "factor"),
Geno = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("G1",
"G2", "G3"), class = "factor"), AlleleA = structure(c(1L,
4L, 4L, 2L, 1L, 4L, 3L, 1L, 4L), .Label = c("AA", "CC", "GG",
"TT"), class = "factor"), AlleleB = structure(c(1L, 4L, 4L,
2L, 1L, 4L, 3L, 1L, 4L), class = "factor", .Label = c("AA",
"CC", "GG", "TT")), AlleleC = structure(c(1L, 4L, 4L, 2L,
1L, 4L, 3L, 1L, 4L), class = "factor", .Label = c("AA", "CC",
"GG", "TT")), AlleleD = structure(c(1L, 4L, 4L, 2L, 1L, 4L,
3L, 1L, 4L), class = "factor", .Label = c("AA", "CC", "GG",
"TT")), AlleleE = structure(c(1L, 4L, 4L, 2L, 1L, 4L, 3L,
1L, 4L), class = "factor", .Label = c("AA", "CC", "GG", "TT"
))), .Names = c("SNP", "Geno", "AlleleA", "AlleleB", "AlleleC",
"AlleleD", "AlleleE"), row.names = c(NA, -9L), class = "data.frame")
在這個問題上,他只有一列想分成兩列。問題是我有5000列(AlleleA,AlleleB .........等),想分裂(每一列到兩列)
我試過使用這樣的循環,但它doesnt工作,
for(i in colnames(dat)){
dat1 <- data.frame(do.call(rbind, strsplit(as.vector(sprintf("dat$%s",i)), split = "")))
}
我會等你的光, 謝謝
如何分割列? (每列只有兩列,分割的定義如何?)。在tidyr中有一個單獨的函數,可以將列分成多列,你可以將它應用到你想要分割的每一列,例如dplyr的mutate_each函數。 – 2014-12-05 09:32:06
@beginneR我修改了我的問題 – user46543 2014-12-05 09:40:28
@beginneR其作品使用splitstackshape :)感謝Ananda Mahto – user46543 2014-12-05 09:45:31