将每两个连续列组合成r中的一列

时间:2015-07-15 18:03:14

标签: r

我有一个这样的数据框:

DF = read.table(text="ID S1 S1 S2 S2 S3 S3 S4 S4 S5 S5
Chr18_2191 C C C T N N T T C C
Chr18_3680 G A A A G G G G G G
Chr18_3881 N N T T A A T T A A
Chr18_4091 G G A G A A G G A A
Chr18_4651 C C T T C C T C C C
Chr18_4705 G G A A G G A A G G
Chr18_4741 G G N N G A G G A A", header=T, stringsAsFactors=F)

我想将每两个连续列组合成一列。结果预计为:

ID S1 S2 S3 S4 S5
Chr18_2191 CC CT NN TT CC
Chr18_3680 GA AA GG GG GG
Chr18_3881 NN TT AA TT AA
Chr18_4091 GG AG AA GG AA
Chr18_4651 CC TT CC TC CC
Chr18_4705 GG AA GG AA GG
Chr18_4741 GG NN GA GG AA

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

这是一个选择:

data.frame( DF[1], mapply( paste0, DF[-1][c(T,F)], DF[-1][c(F,T)] ) )

给出了

          ID S1 S2 S3 S4 S5
1 Chr18_2191 CC CT NN TT CC
2 Chr18_3680 GA AA GG GG GG
3 Chr18_3881 NN TT AA TT AA
4 Chr18_4091 GG AG AA GG AA
5 Chr18_4651 CC TT CC TC CC
6 Chr18_4705 GG AA GG AA GG
7 Chr18_4741 GG NN GA GG AA