0

I created a function for simples characters... my function completes a character with zeros while the number of character is less(strictly) that 4.

completamento<- function(c)
{
  while( nchar(c)< 4)
   {
     c<- paste0("0",c)
   }
return(c)
}   

When I do

completamento(c="aaa")
> "0aaa"

It is OK...

But when I try to put the argument of the function to be one vector, there is some wrong..

completamento(c=c("aaa", "bb"))
Warning messages:
1: In while (nchar(c) < 4) { :
   the condition has length > 1 and only the first element will be used
2: In while (nchar(c) < 4) { :
   the condition has length > 1 and only the first element will be used

There is some way to vectorize this function?

Vasco
  • 585
  • 2
  • 7
  • 25

0 Answers0