在R中最后一次出现特定字符后删除字符

时间:2017-06-21 22:38:28

标签: r regex

我有一个看起来像的字符串:

exampleList <- c("rs40535:1745233:G:A_AGGG","rs41111:1733320:GAC:AAC_TTTTTTG", "exm2344379:1724237:A:T_A", "exm-rs234380:1890910:A:G_A", "rs423444419_T","psy_rs73453432_TCCC","22:1701234072:C:T_C","9:4534345:rs2342342_G","chr10_rs7287862_C","psy_rs7291672_A")  

我想删除最后一个下划线(_)之后的所有内容,所以我的结果看起来像这样:

[1] "rs40535:1745233:G:A"      "rs41111:1733320:GAC:AAC"  "exm2344379:1724237:A:T"   "exm-rs234380:1890910:A:G"   "rs423444419"              "psy_rs73453432"           "22:1701234072:C:T"        "9:4534345:rs2342342"     "chr10_rs7287862"          "psy_rs7291672"    

我已尝试过以下操作,但这会删除第一个_之后的所有内容。

gsub("\\_.*$","",exampleList) 

我认识到有类似的帖子,但我在R中找不到。

1 个答案:

答案 0 :(得分:6)

想出来了!

outcome <- sub("_[^_]+$", "", exampleList)