You need feedback:
test <- "The sample is 22mg" > gsub("([0-9])([a-zA-Z])","\\1 \\2",test) [1] "The sample is 22 mg"
Everything in brackets is remembered. Then they are accessed by \ 1 (for the first object in parens), \ 2, etc. The first backslash skips the interpretation of the backslash in R so that it is passed to the regular expression parser.
Ari B. Friedman
source share