Functions printf and sprintf are borrowed from c programming language and you need R.utils package imported in order to use them.
Both those functions require format string where, with %s function requests string as argument, with %d- number, %f- number with floating point, and with %x argument is going to be interpreted as byte and presented in hexadecimal format.
printf("%s\t%d\t%x\t%.3f", "hey", 12, 10, 25.2698)
Result from printing function above is going to be:
hey 12 a 25.269
With formatting string with %s we demanded that our first argument must be a string, and string that we provided is "hey". With \t we are telling that we need 4 empty spaces in between our arguments. Then with %d we demanded integer and we provided 12 to it. Next was %x and we provided 10; however a is printed; that is because a is 10 in hexadecimal form. Finally instead of 25.2698 that we provided as final argument, 25.269 is printed; because with formatting argument of %.3f we said that we need floating point number with 3 decimal places, so fourth decimal place is ignored.
Function sprintf does the same as function printf, but instead of printing resulting string to the screen, result is assigned to a variable.
a <- sprintf("%s\t%d\t%x\t%.3f", "hey", 12, 10, 25.2698)
For video tutorial, check the link below:
No comments:
Post a Comment