I have a vector of strings a<-data.table(value=("001","01"))
that really should be decimals (0.01 and 0.1) and would liked to split the strings at the first 0 and add in a decimal before it.
I can split the string and get a list
A<-strsplit(a$value, "(?=[0])", perl = TRUE)
so I can envisage doing paste0(x[[1]][1],".",x[[1]][2]) inside a function but I'm getting a bit stuck with how to refer to the indices:
dFun<-function(){
as.numeric(paste0(x[[1]][1],".",x[[1]][2]))
}
purrr::map_df(.x=1:2,.f=dFun)
Any pointers would be much appreciated.
Maybe you can try this
> a[, value := gsub("(?=1)", ".", value, perl = TRUE)][]
value
1: 00.1
2: 0.1
or
> a[, value := sub("(^0)", "\\1.", value)][]
value
1: 0.01
2: 0.1
or
> a[, value := as.numeric(sub("(^0)", "\\1.", value))][]
value
1: 0.01
2: 0.10