Alternative with stringr, presumably the name of the vector x
library(stringr) str_sub(x,1,str_locate(x,"-")[ ,1])
this part takes a vector of strings as an argument, returns the position of the matching pattern in this case, β-β in the string
str_locate(x,"-")
Thus, this code will return a matrix of start and end positions, which in this case are the same numbers, because "-" is only one character, starting and ending at the same position
start end [1,] 3 3 [2,] 4 4 [3,] 3 3
When we multiply this path
str_locate(x,"-")[ ,1]
we get
[1] 3 4 3
and now the str_sub function receives a substring of the entire string, where we indicate the start and end position of the substring. Thus, basically it is read in the same way as for all elements of the vector x, creating a substring starting with character 1 and ending at the position of the first dash, which is calculated as shown earlier.
str_sub(x,1,str_locate(x,"-")[ ,1])
source share