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?✗ Incorrect
The %.3f specifier formats a number as a float with 3 decimal places.
What will
sprintf("Hello %s", "world") output?✗ Incorrect
The %s is replaced by the string "world", so the output is "Hello world".
How do you print a percent sign (%) using
sprintf?✗ Incorrect
Double percent signs (%%) print a single percent sign in the output.
What does
sprintf("%d", 3.7) output?✗ Incorrect
The %d specifier converts the number to an integer by truncating the decimal part.
If you have fewer values than format specifiers in
sprintf, what happens?✗ Incorrect
Missing values for format specifiers cause an error or warning because
sprintf expects matching values.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.