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?
You can as well use stringr
library(stringr)
v2<-str_extract(v, "[^-]+")
The following regex will do it.
sub("(^[^-]+)-.*", "\\1", Name)
#[1] "W1" "Empty" "W2"
Data.
Name <- scan(what = character(), text ="
W1-D1
Empty
W2-D1
")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With