I'm creating directories and files for a personal project. Each is going to be associated with a bus line (the scope of the project is transportation).
This lead me to this problem. How can I transform a vector of int, that contains bus line values, to a vector of strings? e.g: [3,100,700,5,701] - > ["0003", "0100", "0700", "701"]
The idea is so the project will be very organized ( >.< )
I have done a function, but it is not the best. Please don't judge.
numeric.to.character.digits <- function(numeric.vector){
vet.character <- c()
for(i in 1:length(numeric.vector))
{
if(numeric.vector[i]/1000 >=1)
{
vet.character[i] <- as.character(numeric.vector[i])
}
if (numeric.vector[i]/1000 < 1)
{
vet.character[i] <- paste0("0",as.character(numeric.vector[i]))
}
if(numeric.vector[i]/100 < 1)
{
vet.character[i] <- paste0("00",as.character(numeric.vector[i]))
}
if(numeric.vector[i]/10 < 1)
{
vet.character[i] <- paste0("000",as.character(numeric.vector[i]))
}
}
return (vet.character)
}
But, this function is not complete. I wish to have a more generic one. In this one I made, I can only input bus lines with 4 digits. Isn't there a more generic way of implementing it? The number of '0' has to be defined in terms of the number of maximum digits.
E.g: [1000,1,10] -> max digits = 4 -> ['1000','0001','0010']