Consider the input c("foo 1", "bar 2", "baz"). I’d like to turn this into c(1,2,NA) (basically extract the numbers from each string, or if none exist turn it into an NA). My first pass looks like this:
funNums = as.numeric(
regmatches(x$Fun,
regexpr('\\d+', x$Fun, perl = T)))
where x$Fun is my input vector. The output I get from this though is c(1,2) since regmatches throws away things which don’t match. How can I get it to include NAs?
(Do be aware that when passed a string like
"1 to 2", this will return the number12, which may not be what you’d like it to do.)