'Formating in r: print(sprintf("%03d / %03d")
I'm a beginner in R and I'm confused by formatting part of this code:
print(sprintf("%03d / %03d", j, n)).
I familiarized myself with ?sprintf and r documentation. I understand each sign but I don't understand how to read it all together especially with '/'. What does this formatting mean?
Solution 1:[1]
The "/" is just a literal forward slash that should appear in the output string.
To break the code down, it means "Write the integer j with leading zeros if necessary, so that it is at least 3 characters long (%03d), then write the literal string " / ", then write the integer n with leading zeros if necessary so that it is at least 3 characters long (%03d)"
For example:
sprintf("%03d / %03d", 4, 2)
#> [1] "004 / 002"
In other words, the forward slash could be any text:
sprintf("%03d banana %03d", 4, 2)
#> [1] "004 banana 002"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
