使用模式匹配重命名colnames

时间:2016-04-05 11:35:11

标签: regex r

如何通过模式匹配重命名组合名称;只保留从第2列到第4列以“B”开头的字符串

colnames(j)

      "ensembl_gene_id"  "Sample_3605_1_B1_1321584_RC" "Sample_3605_2_B2_1321584_Eso" "Sample_3605_5_B14_1326370_RC" "Sample_3605_8_B19_1326383_RC"  "chromosome_name"  "start_position"  "end_position"

预期产出

  "ensembl_gene_id"  "B1"  "B2" "B14"  "B19" "chromosome_name" "start_position"                  "end_position" 

我试过

j[2:4] <- gsub(pattern = "^Sample_3605_[[:digit:]]","",j[2:4])    

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

j[2:4] <- gsub(pattern = "^Sample_3605_[[:digit:]]_(B[^_]*).*","\\1",j[2:4])    

答案 1 :(得分:1)

当然还有stringr解决方案

library(stringr)
str_extract(colnames(j)[2:4], 'B[0-9]+')