Probably a bit more robust/versatile/safe than the solution suggested by @r2evans in the comments.
gsub( "(\\d{2,})\\d{2}$", "\\1", df)
what it does:
pattern = "(^\\d{2,})\\d{2}$"
^
matches the start of the string
\\d{2,}
matches any substring of at least two digits (delete the comma of you only want to match strings of the exact length of 4 digits)
(^\\d{2,})
the round brackets define the start from the string and the following repetition of minimal two digits as a group.
\\d{2}
a repetition of exactly two digits
$
matches the end of a string
in short: it matches any string that exits solely of digits, that starts with a minimum of two digits, andd ends with two digits (so the minimum length of the digit string = 4)
replacement = "\\1"
- replaces the entire matches string woth the first defind group (
(^\\d{2,})
) from the above described pattern.
sample data
df <- c(123, "Other", 5678, "Abstain", "b12345", 123456, "123aa345")
gsub("(^\\d{2,})\\d{2}$", "\\1", df)
#[1] "123" "Other" "56" "Abstain" "b12345" "1234" "123aa345"