'How to select specific characters from input in R?

I have a vector with data of the form

c("AM1-Project", "AM14-Blend", "B5-Implement", "SS10-Review")

and I would like to select only the characters until "-", so the ouptut should be

c("AM1", "AM14", "B5", "SS10") 


Solution 1:[1]

You could use sub here:

x <- c("AM1-Project", "AM14-Blend", "B5-Implement", "SS10-Review")
output <- sub("-.*", "", x)
output

[1] "AM1"  "AM14" "B5"   "SS10"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Biegeleisen