I am currently trying to crack the seemingly simple problem in R but somehow I am unable to find a way to do it with gsub, str_match() or some other rgex-related functions. Can anyone please help me crack this problem?
Problem Assuming that I have a column vector of certain length (say, 100). Each element in a vector has the form of [string]_[string+number]_[someinfo]. Now, I want to extract only the very first part of each element, namely the [string]_[string+number]. The potential upper bound on the number of characters in [string]_[string+number], not including _, could be anywhere between 8 and 20, but there is no fixed length. How can I use some types of rgex expression to do this in R?
x = c('XY_ABCD101_12_ACE', 'XZ_ACC122_100_BAN', 'XT_AAEEE100_12345_ABC', 'XKY_BBAAUUU124_100')
Desired output.
x1 = c('XY_ABCD101', 'XZ_ACC122', 'XT_AAEEE100', 'XKY_BBAAUUU124')