We can use sub
to extract the numbers before the first -
by matching the -
followed by one or more characters (.*
) till the end of the string, replace it with ""
, convert it to numeric
(as.numeric
), as well as extract the substring from the first instance of -
till the end of the string by matching one or more characters that are not a -
([^-]+
) from the start of the string, replace it with ""
. Use these substrings as arguments in sprintf
with the correct fmt
to paste it together.
df1$V1 <- sprintf("%06d%s", as.numeric(sub("\\-.*", "", df1$V1)), sub("^[^-]+", "", df1$V1))
df1
# V1
#1 000098-45.3A-22
#2 000104-44.0A-23
#3 000983-29.1-22
#4 001757-42.5A-22
#5 004968-37.3A2-23
We can also do this in a single step using gsubfn
. Here, we match the numbers (\\d+
) at the start (^
) of the string, capture it as a group, in the replacement, convert that the captured group into numeric
and change the format with sprintf
library(gsubfn)
gsubfn("^(\\d+)", ~sprintf("%06d", as.numeric(x)), df1$V1)
#[1] "000098-45.3A-22" "000104-44.0A-23" "000983-29.1-22"
#[4] "001757-42.5A-22" "004968-37.3A2-23"
data
df1 <- structure(list(V1 = c("98-45.3A-22", "104-44.0A-23", "983-29.1-22",
"1757-42.5A-22", "4968-37.3A2-23")), .Names = "V1", class = "data.frame",
row.names = c(NA, -5L))
solved Add leading zeros before first hypen [closed]