0

I have a column in my dataframe with numerical values up to 6 digits - e.g., 145, 137549, 2355, etc. - and I need to convert the data into a character string format that starts with "PV" and ends with 6 digits. So, 145 -> "PV000145" and 137549 -> "PV137549" so the conversion is smart enough to add leading zeroes where needed. How can I do this in R?

I tried the format function but can't figure out the correct inputs to add PV every time and still retain the number that is already in the field.

1 Answers1

2
vec <- c(145, 137549, 2355)
sprintf("PV%06d", vec)
# [1] "PV000145" "PV137549" "PV002355"

%06d is a 0-padded (out to 6 digits) integer, see ?sprintf for that and other %-codes.

If your vec is already character (I'll convert the above for an example using vec_chr <- as.character(vec)), then we can use stringr::str_pad and paste0:

paste0("PV", stringr::str_pad(vec_chr, 6, pad = "0"))
# [1] "PV000145" "PV137549" "PV002355"

or without stringr in another step or two:

paste0("PV", strrep("0", max(nchar(vec_chr)) - nchar(vec_chr)), vec_chr)
# [1] "PV000145" "PV137549" "PV002355"
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • thank you, i can create the vector that converts the field but having trouble getting those values back into the original dataframe. is there way to do it all in one, maybe with a mutate function? – Ann Marie Palmieri Aug 29 '23 at 16:59
  • 1
    Doesn't "normal reassignment" work? I'd think `dat$vec <- sprintf("%06d", dat$vec)` would work. I know nothing of your real data, please edit your original question and add the output from `dput(head(dat))` where `dat` is your frame. (See https://stackoverflow.com/q/5963269 , [mcve], and https://stackoverflow.com/tags/r/info for various discussions on how to include data and make the question _reproducible_.) Thanks! – r2evans Aug 29 '23 at 17:08
  • thank you, yes i was able to get it to work using the following: vec <- sprintf("PV%06d", turnAL$`Masterlist code`) turnAL$`Masterlist code` <- vec – Ann Marie Palmieri Aug 30 '23 at 17:41
  • Cool. What about cutting out `vec` and doing just `turnAL$\`Masterlist code\` <- sprintf("PV%06d", turnAL$\`Masterlist code\`)`? – r2evans Aug 30 '23 at 17:42
  • Excellent! thanks for streamlining – Ann Marie Palmieri Aug 30 '23 at 17:56