There is a (more general) regex approach that does not require specifying the width of the leading line:
Or delete anything you want, and after underlining:
> as.numeric(factor(sub("_.+", "" , x)))
[1] 1 1 2 3 3
, ( R- , parens, "\\", ):
> as.numeric(factor(sub("(^.+)_.+$", "\\1" , x)))
[1] 1 1 2 3 3