Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract string before dash in R

Tags:

string

r

extract

I have a column for name, the format is mixed with AAA and AAA-D. I want to extract the name before dash (if it has dash) or keep the non-dashed name. the list is

Name
W1-D1
Empty
W2-D1

what I want to extract are

Name
W1
Empty
W2

I found several syntaxes like v1<-gsub("^(.*?)-.*", "\\1",v) but this does not work on my list, I got this “c(\"W1" in v1 . Did I use this syntax wrong?

like image 494
4 revs Avatar asked May 22 '26 07:05

4 revs


2 Answers

You can as well use stringr

library(stringr)

v2<-str_extract(v, "[^-]+")
like image 106
Kemboi Ian Kibet Avatar answered May 23 '26 19:05

Kemboi Ian Kibet


The following regex will do it.

sub("(^[^-]+)-.*", "\\1", Name)
#[1] "W1"    "Empty" "W2" 

Data.

Name <- scan(what = character(), text ="
W1-D1
Empty
W2-D1
")
like image 32
Rui Barradas Avatar answered May 23 '26 20:05

Rui Barradas