String formatting with sprintf
📖 Scenario: You are preparing a report that shows the prices of fruits in a neat format. You want to display the fruit name and its price with exactly two decimal places.
🎯 Goal: Build a small R program that uses sprintf to format fruit prices as strings with two decimal places.
📋 What You'll Learn
Create a character vector called
fruits with the values "Apple", "Banana", and "Cherry".Create a numeric vector called
prices with the values 1.2, 0.5, and 2.35.Create a format string variable called
format_str with the value "%s: $%.2f".Use
sprintf with format_str, fruits, and prices to create a character vector called formatted_prices.Print the
formatted_prices vector.💡 Why This Matters
🌍 Real World
Formatting prices or other data neatly is common in reports, invoices, and user interfaces.
💼 Career
Knowing how to format strings precisely is useful for data presentation and creating readable outputs in data analysis and software development.
Progress0 / 4 steps