Recap - Sales Report
Part of the Logic & Flow section of Coddy's R journey. Lesson 46 of 64.
Challenge
MediumComplete sales_report(df). df has the columns region, product, units and price. Build the report:
- Add a
revenuecolumn: units times price. - Keep the rows with at least one unit sold.
- Return one line per region in alphabetical order,
north: 45.00, with the region's total revenue. - Add
Top product:followed by the product with the highest revenue in a single row, andRows used:followed by the number of rows kept.
When several rows share the highest revenue, use the first of them. Show money with two decimals.
The supplied code reads the table from the input with read.csv(text = input): the first line holds the column names and every following line is one row, with values separated by commas. The code prints each returned line.
Try it yourself
sales_report <- function(df) {
# Write your code here
character(0)
}
# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
df <- read.csv(text = input)
cat(sales_report(df), sep = "\n")
All lessons in Logic & Flow
1Strings In Depth
Substrings with substr()Formatting with sprintf()Splitting and JoiningSearching StringsReplacing TextRecap - Username Builder4Matrices
Creating MatricesIndexing MatricesRow and Column SummariesMatrix ArithmeticRecap - Seating Chart2Key-Value Lookups
Named Vector LookupsChecking KeysAdding and Removing KeysLooping Over NamesRecap - Stock Desk3Sets and Counting
Unique ValuesSet OperationsMembership TestsCounting with table()Recap - Event Guests6Functions as Values
Anonymous FunctionsPassing FunctionsReturning FunctionsClosures with StateRecap - Discount Rules9Data Frames
Creating Data FramesColumns and RowsFiltering RowsAdding and SortingRecap - Sales Report12Project - Expense Tracker
Recording ExpensesTotal SpendingPractice on your own: Online R compiler