If you have more unpleasant lines in which periods may appear outside the line part number, you can use something like this.
library(gsubfn) ## A possibly more realistic character vector x <- c("2.11abc.def","ab-15.1cde",".562342rfg", "abcdef") getNumberPart <- function(x) { pat <- "(-?(\\d*\\.*\\d+|\\d+\\.))" strapply(x, pattern=pat, FUN=as.numeric, simplify=TRUE, empty=NA) } getNumberPart(x) # [1] 2.110000 -15.100000 0.562342 NA
Note that if a string contains more than one number, strapply() will extract all of them and return the overall result as a list (rather than a simple vector) with one list item on the input string.
Josh o'brien
source share