Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a string that looks like:

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")  

I wish to remove everything after the last underscore ( _ ) so my result looks something like this:

[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"    

I've tried the following, but this removes everything after the first _.

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

I recognize there are similar posts but none I could find in R.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
754 views
Welcome To Ask or Share your Answers For Others

1 Answer

Figured it out!

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...