R命令以增量替代

时间:2018-08-22 01:36:46

标签: r

我想在下面替换:

[1] "annotated_SRR7059136_CD39-.txt" "annotated_SRR7059137_CD39+.txt"
[3] "annotated_SRR7059138_CD39-.txt" "annotated_SRR7059139_CD39+.txt"
[5] "annotated_SRR7059140_CD39-.txt" "annotated_SRR7059141_CD39+.txt"
[7] "annotated_SRR7059142_CD39-.txt" "annotated_SRR7059143_CD39+.txt"

进入

[1] "1_CD39-.txt" "1_CD39+.txt"
[3] "2_CD39-.txt" "2_CD39+.txt"
[5] "3_CD39-.txt" "3_CD39+.txt"
[7] "4_CD39-.txt" "4_CD39+.txt"

到目前为止,我已经尝试运行此

x<- sub("annotated_SRR70591.*_","\\1",sampleFiles)

但是我得到

[1] "CD39-.txt" "CD39+.txt" "CD39-.txt" "CD39+.txt" "CD39-.txt" "CD39+.txt"
[7] "CD39-.txt" "CD39+.txt"

而不是我想要的。任何帮助将非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我们可以使用Select-Objectrep生成数字并seq一起生成数字。

paste

为了更好地理解分解步骤,

paste0(rep(seq(length(sampleFiles)/2), each = 2), "_", 
           sub("annotated_SRR70591.*_","\\1",sampleFiles))

#[1] "1_CD39-.txt" "1_CD39+.txt" "2_CD39-.txt" "2_CD39+.txt" "3_CD39-.txt" 
#[6] "3_CD39+.txt" "4_CD39-.txt" "4_CD39+.txt"

rep(seq(length(sampleFiles)/2), each = 2) #gives
#[1] 1 1 2 2 3 3 4 4