Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Returning all characters after the first underscore

Tags:

r

gsub

Sample DATA

x=c("AG.av08_binloop_v6","TL.av1_binloopv2")

Sample ATTEMPT

y=gsub(".*_","",x)

Sample DESIRED

WANT=c("binloop_v6","binloopv2")

Basically I aim to extract all the characters AFTER the first underscore value.

like image 337
bvowe Avatar asked Dec 07 '22 12:12

bvowe


1 Answers

In the pattern, we can change the zero or more any characters (.* - here . is metacharacter that can match any character) to zero or more characters that is not a _ ([^_]*) from the start (^) of the string.

sub("^[^_]*_", "", x)
#[1] "binloop_v6" "binloopv2" 

If we don't specify it as such, the _ will match till the last _ in the string and uptill that substring will be lost returning 'v6' and 'binloopv2'


An easier option would be word from stringr

library(stringr)
word(x, 2, sep = "_")
#[1] "binloop"   "binloopv2"
like image 172
akrun Avatar answered Dec 27 '22 21:12

akrun