Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing integer 7 as a character "007" in R

I am trying to convert integer into string by using as.character function in R. But I need the string to be always of length 3. So 7 will get converted into 007, 10 into 010 and so on. I am wondering if there is some simple modification of as.character (or any other built-in R routine) which will quickly accomplish this.

like image 438
hardikudeshi Avatar asked Dec 01 '22 22:12

hardikudeshi


1 Answers

See ?sprintf:

R> sprintf("%03d", c(7, 10))
[1] "007" "010"
like image 103
rcs Avatar answered Dec 04 '22 00:12

rcs