我想要合并4列。
CHR POS EFFECT_ALLELE NON_EFFECT_ALLELE
1 124 A C
5 378 C T
3 398 T C
3 564 G A
我希望它们以下列方式合并:
CHR:POS:EFFECT_ALLELE:NON_EFFECT_ALLELE
然而,我想" EFFECT_ALLELE:NON_EFFECT_ALLELE"按字母顺序合并。
因此,对于上面的行,它将是:
1:124:A:C
5:378:C:T
3:398:C:T
3:564:A:G
所以,对于上面的第三个和第四个例子,它必须是" NON_EFFECT_ALLELE:EFFECT_ALLELE'
任何人都可以帮我编写合并代码" NON_EFFECT_ALLELE"和" EFFECT_ALLELE"按字母顺序排列?
提前致谢!
答案 0 :(得分:0)
我们可以先sort
数据并执行paste
df1[3:4] <- t(apply(df1[3:4], 1, sort))
do.call(paste, c(df1, sep=":"))
#[1] "1:124:A:C" "5:378:C:T" "3:398:C:T" "3:564:A:G"
或者这可以通过矢量化方式完成,而不是循环遍历每一行来执行sort
with(df1, paste(CHR, POS, pmin(EFFECT_ALLELE, NON_EFFECT_ALLELE),
pmax(EFFECT_ALLELE, NON_EFFECT_ALLELE), sep=":"))
#[1] "1:124:A:C" "5:378:C:T" "3:398:C:T" "3:564:A:G"