如何将数据框中的每一列拆分为两列?

How to split each of the columns in a data frame to two column?

我有一个这样的数据框(4 行 5 列):

Marker ind1 ind2 ind3 ind4
mark1             CT             TT             CT             TT
mark2             AG             AA             AG             AA
mark3             AC             AA             AC             AA
mark4             CT             TT             CT             TT

我想要做的是将每一列(第一列除外)拆分为两列。所以输出应该是这样的(4 行和 9 列):

Marker ind1 ind1 ind2 ind2 ind3 ind3 ind4 ind4
mark1             C T             T T             C T             T T
mark2             A G             A A             A G             A A
mark3             A C             A A             A C             A A
mark4             C T             T T             C T             T T

我知道如何拆分一栏

do.call(rbind,strsplit(test$JRP4RA6119.039, ""))

这给出了这个:

      [,1] [,2]
 [1,] "C"  "T" 
 [2,] "A"  "G" 
 [3,] "A"  "C" 
 [4,] "C"  "T" 

我想要的是能够循环它并为一个数据帧中的所有列制作它。

提前致谢。

感觉有点牵强但是:

test_split <- data.frame(Marker=test$Marker, 
                         do.call("cbind", lapply(apply(test[, -1], 2, strsplit, ""), 
                                                 function(x) do.call("rbind", x))), 
                         stringsAsFactors=F)
colnames(test_split)[-1] <- paste(rep(colnames(test)[-1], e=2), 1:2, sep="_")

test_split
#      Marker JRP4RA6119.039_1 JRP4RA6119.039_2 JRP4RA6124.029_1 JRP4RA6124.029_2 JRP4RA6133.051_1 JRP4RA6133.051_2 JRP4RA6125.009_1 JRP4RA6125.009_2
#1 s7e4419xxx                C                T                T                T                C                T                T                T
#2 s7e7001s01                A                G                A                A                A                G                A                A
#3 s7e3049xxx                A                C                A                A                A                C                A                A
#4 s7e4727xxx                C                T                T                T                C                T                T                T
> b <- as.data.frame(a[, 1])
> b[, 2] <- substr(a[, 2], 1, 1)
> b[, 3] <- substr(a[, 2], 2, 2)
> b[, 4] <- substr(a[, 3], 1, 1)
> b[, 5] <- substr(a[, 3], 2, 2)
> b[, 6] <- substr(a[, 4], 1, 1)
> b[, 7] <- substr(a[, 4], 2, 2)
> b[, 8] <- substr(a[, 5], 1, 1)
> b[, 9] <- substr(a[, 5], 2, 2)
> head(b)
  a[, 1] V2 V3 V4 V5 V6 V7 V8 V9
1  mark1  C  T  T  T  C  T  T  T
2  mark2  A  G  A  A  A  G  A  A
3  mark3  A  C  A  A  A  C  A  A
4  mark4  C  T  T  T  C  T  T  T
> dim(b)
[1] 4 9
> names(b) <- c("Marker", "ind1", "ind1","ind2", "ind2", "ind3", "ind3", "ind4", "ind4")
> head(b)
  Marker ind1 ind1 ind2 ind2 ind3 ind3 ind4
1  mark1    C    T    T    T    C    T    T
2  mark2    A    G    A    A    A    G    A
3  mark3    A    C    A    A    A    C    A
4  mark4    C    T    T    T    C    T    T
  ind4
1    T
2    A
3    A
4    T
> 

您可以轻松地将它变成一个循环,但由于列数相对较少,我没有必要这么做。

要使其成为循环,只需将其设置为

for(i in 2:ncol(a)){
}

您也可以尝试从 splitstackshape

cSplit_f
library(splitstackshape)
df1[-1] <- lapply(df1[-1] , function(x)
        gsub('(?<=\w)(?=\w)', ',', x, perl=TRUE))
 cSplit_f(df1, 2:ncol(df1), sep=',')
#   Marker ind1_1 ind1_2 ind2_1 ind2_2 ind3_1 ind3_2 ind4_1 ind4_2
#1:  mark1      C      T      T      T      C      T      T      T
#2:  mark2      A      G      A      A      A      G      A      A
#3:  mark3      A      C      A      A      A      C      A      A
#4:  mark4      C      T      T      T      C      T      T      T

或者按照@Ananda Mahto的建议,cSplit在大型数据集上可能更高效,这个可以直接使用,无需更改分隔符。

cSplit(df1, names(df1)[-1], sep="", stripWhite = FALSE)
#   Marker ind1_1 ind1_2 ind2_1 ind2_2 ind3_1 ind3_2 ind4_1 ind4_2
#1:  mark1      C      T      T      T      C      T      T      T
#2:  mark2      A      G      A      A      A      G      A      A
#3:  mark3      A      C      A      A      A      C      A      A
#4:  mark4      C      T      T      T      C      T      T      T

或使用 data.table

中的 tstrsplit
library(data.table)#v1.9.5+
setDT(df1)
cbind(Marker=df1$Marker,df1[, unlist(lapply(.SD, function(x)
        tstrsplit(x, '')), recursive=FALSE), .SDcols=-1])
#   Marker ind11 ind12 ind21 ind22 ind31 ind32 ind41 ind42
#1:  mark1     C     T     T     T     C     T     T     T
#2:  mark2     A     G     A     A     A     G     A     A
#3:  mark3     A     C     A     A     A     C     A     A
#4:  mark4     C     T     T     T     C     T     T     T

数据

df1 <- structure(list(Marker = c("mark1", "mark2", "mark3", "mark4"), 
ind1 = c("CT", "AG", "AC", "CT"), ind2 = c("TT", "AA", "AA", 
"TT"), ind3 = c("CT", "AG", "AC", "CT"), ind4 = c("TT", "AA", 
"AA", "TT")), .Names = c("Marker", "ind1", "ind2", "ind3", 
"ind4"), class = "data.frame", row.names = c(NA, -4L))