[R] Extracting a numeric prefix from a string

Mike White mikewhite.diu at tiscali.co.uk
Mon Jan 31 09:51:59 CET 2005


Hi
Does anyone know if there is a function similar to as.numeric that will
extract a numeric prefix from a string as in the following examples?

x<-c(3, "abc", 5.67, "2.4a", "6a", "6b", "2.4.a", 3, "4.2a")
df.x<-data.frame(Code=x)
x.str<-levels(df.x[,1])
# required function  result
2.40 3.00 4.20 5.67 6.00 NA

Thanks
Mike White




More information about the R-help mailing list