简体   繁体   中英

Removing everything after `*`

I was wondering how to remove everything after a * in my data. I have values such as: IGHV4-59*01 and IGHV4-4*02 and wanted just the IGHV4-59 and IGHV-4 . I tried using the sub but did not get the desired result:

sub("*.*" , " ", data_head$v_segment)
## [1] " " " " " " " " " " " "

Instead of removing everything after the * it removed everything entirely. Thanks.

Try this. Basically, * is a metacharacter in regex, and thus you need to escape it if you want to the engine to treat it as an actual punctuation character.

gsub("\\*.*", "", c("IGHV4-59*01", "IGHV4-4*02")) # You can use `sub` too as per comment
## [1] "IGHV4-59" "IGHV4-4" 

Your regex failed because * is a special character in a regular expression even when there is no other character in front of it. Therefore it needs to be escaped. "\\\\*.*" is the pattern you want.

它没有按预期工作,因为您需要转义*以匹配文字。

sub("\\*.*", "", data_head$v_segment)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM