I have slightly modified the jangorecki function for the case where we may have a variety of values that cannot be converted to a number. In my function, a template search is performed and if the template is not found, FALSE is returned.! before gperl, it means that we need those vector elements that do not match the template. The rest is similar to the as.num
function. Example:
as.num.pattern <- function(x, pattern){
stopifnot(is.character(x))
na = !grepl(pattern, x)
x[na] = -Inf
x = as.numeric(x)
x[na] = NA_real_
x
}
as.num.pattern(c('1', '2', '3.43', 'char1', 'test2', 'other3', '23/40', '23, 54 cm.'))
[1] 1.00 2.00 3.43 NA NA NA NA NA