I have a vector
x <- c(0, 25, 0, 75, 50, 50, 0)
I would like to convert it so the 0's and only the zeroes are double, as so:
x <- c(00, 25, 00, 75, 50, 50, 00)
Anyone have a neat idea how to do this?
CodePudding user response:
x <- c(0, 25, 0, 75, 50, 50, 0)
sprintf('.0f', x)
sprintf('%.2d', x)
sprintf('i', x)
Will all yield identical output
CodePudding user response:
You did not specify a format, but you can do this:
sprintf("%.2d", x)
# [1] "00" "25" "00" "75" "50" "50" "00"
However they will be in character format.
CodePudding user response:
Using str_pad
library(stringr)
str_pad(x, pad = '0', width = 2)
[1] "00" "25" "00" "75" "50" "50" "00"