0
0
R Programmingprogramming~5 mins

String formatting with sprintf in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the sprintf function do in R?
It creates a formatted string by inserting values into a template, similar to filling blanks in a sentence.
Click to reveal answer
beginner
How do you format a number to show exactly 2 decimal places using sprintf?
Use %.2f in the format string, like sprintf("%.2f", 3.14159) which outputs "3.14".
Click to reveal answer
beginner
What does the format specifier %d mean in sprintf?
It means to format the value as an integer (whole number).
Click to reveal answer
intermediate
How can you include a literal percent sign (%) in the output of sprintf?
Use %% in the format string to print a single percent sign.
Click to reveal answer
intermediate
What happens if you provide more values than format specifiers in sprintf?
Extra values are ignored; only values matching the format specifiers are used.
Click to reveal answer
Which sprintf format specifier would you use to format a floating-point number with 3 decimal places?
A%.3d
B%d
C%s
D%.3f
What will sprintf("Hello %s", "world") output?
AHello %s
BHello world
Cworld Hello
DError
How do you print a percent sign (%) using sprintf?
AUse %%
BUse %p
CUse %s
DUse %percent
What does sprintf("%d", 3.7) output?
A4
B3.7
C3
DError
If you have fewer values than format specifiers in sprintf, what happens?
AError or warning
BIt fills missing values with zero
CIt ignores missing values
DIt repeats last value
Explain how to use sprintf to format a string with a name and a number showing two decimals.
Think about placeholders for text and numbers.
You got /3 concepts.
    Describe how to print a literal percent sign (%) using sprintf and why it requires special syntax.
    Percent signs are used to mark placeholders.
    You got /3 concepts.