我有一个看起来像的字符串:
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中找不到。
最佳答案
弄清楚了!
outcome <- sub("_[^_]+$", "", exampleList)