I\'m using the gsub function in R to return occurrences of my pattern (reference numbers) on a list of text. This works great unless no match is found, in whic
gsub
I'd probably go a different route, since the sapply doesn't seem necessary to me as these functions are vectorized already:
sapply
fun <- function(x){ ind <- grep(".*(Ref. (\\d+)).*",x,value = FALSE) x <- gsub(".*(Ref. (\\d+)).*", "\\1", x) x[-ind] <- "" x } fun(data)