regex - Replace in a string with a numeric function in R -


i have string vector:

vec<-c("0","[0.046-0.112)","[0.112-0.141)","[0.141-0.177)","[0.177-0.206)",">=0.206") 

i'd transform decimal numbers inside character vector in percent numbers. use package stingr elab text regex. idea thing like:

str_replace_all(vec, "([0-9.]{5})", paste(as.numeric("\\1")*100,"%") ) 

this i'd expect:

"0%" "[4.6%-11.2%)" "[11.2%-14.1%)" "[14.1%-17.7%)" "[17.7%-20.6%)" ">20.6%"    

but output

[1] "0"           "[na %-na %)" "[na %-na %)" "[na %-na %)" "[na %-na %)" [6] ">=na %"      warning message: in paste(round(as.numeric("\\1") * 100, 1), "%") :   nas introduced coercion 

you can try

library(gsubfn) gsubfn('[0-9.]+', ~ paste0(100*as.numeric(x), '%'), vec) #[1] "0%"            "[4.6%-11.2%)"  "[11.2%-14.1%)" "[14.1%-17.7%)" #[5] "[17.7%-20.6%)" ">=20.6%"       

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -