Вы можете сделать вывод только 4 и 24 с помощью unlist:
unlist(gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired")) [1] 4 24