I run into a problem when converting character of percentage to numeric. E.g. I want to convert \"10%\" into 10%, but
as.numeric(\"10%\")
r
Get rid of the extraneous characters first:
topct <- function(x) { as.numeric( sub("\\D*([0-9.]+)\\D*","\\1",x) )/100 }
my.data <- paste(seq(20)/2, "%", sep = "")
> topct( my.data )
[1] 0.005 0.010 0.015 0.020 0.025 0.030 0.035 0.040 0.045 0.050 0.055 0.060 0.065 0.070 0.075 0.080
[17] 0.085 0.090 0.095 0.100
(Thanks to Paul for the example data).
This function now handles: leading non-numeric characters, trailing non-numeric characters, and leaves in the decimal point if present.