In my data processing, I need to do the following:
#convert '7-25' to '0007 0025'
#pad 0's to make each four-digit number
digits.formatter <- function ('7-25'){.......?}
I have no clue how to do that in R. Can anyone help?
In base R, split the character string (or vector of strings) at -
, convert its parts to numeric, format the parts using sprintf
, and then paste them back together.
sapply(strsplit(c("7-25", "20-13"), "-"), function(x)
paste(sprintf("%04d", as.numeric(x)), collapse = " "))
#[1] "0007 0025" "0020 0013"
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