I have a set of data which looks something like this:
anim <- c(25499,25500,25501,25502,25503,25504)
sex <- c(1,2,2,1,2,1)
wt <- c(0.8,1.2,1.0,2.
Here is another alternative for adding leading to 0s to strings such as CUSIPs which can sometimes look like a number and which many applications such as Excel will corrupt and remove the leading 0s or convert them to scientific notation.
When I tried the answer provided by @metasequoia the vector returned had leading spaces and not 0
s. This was the same problem mentioned by @user1816679 -- and removing the quotes around the 0
or changing from %d
to %s
did not make a difference either. FYI, I am using RStudio Server running on an Ubuntu Server. This little two-step solution worked for me:
gsub(pattern = " ", replacement = "0", x = sprintf(fmt = "%09s", ids[,CUSIP]))
using the %>%
pipe function from the magrittr
package it could look like this:
sprintf(fmt = "%09s", ids[,CUSIP]) %>% gsub(pattern = " ", replacement = "0", x = .)
I'd prefer a one-function solution, but it works.