如何 select 来自 R 中输入的特定字符?

How to select specific characters from input in R?

我有一个包含以下形式数据的向量

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

并且我想 select 只有字符直到“-”,所以输出应该是

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

您可以在此处使用 sub

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

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