Surely a naive question but I'm in R and I have the expression
v <- "gastrula:75%"
that I want to replace with "gastrula0.75"
I tried things like :
v <- sub("\\.(\\d+)%","0.\\1",v)
v <- sub("[:punct:](\\d)\\1+[:punct:]","0.\\1",v)
But I didn't find anything that worked.
You may try
sub('^([^:]+):(\\d+).*', '\\10.\\2', v)
#[1] "gastrula0.75"
Or may be
library(gsubfn)
gsubfn(':(\\d+)%', ~as.numeric(x)/100, v)
#[1] "gastrula0.75"
v1 <- c(v, 'gastrula:5%')
gsubfn(':(\\d+)%', ~as.numeric(x)/100, v1)
#[1] "gastrula0.75" "gastrula0.05"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With