Recap - Data Pipeline
Part of the Logic & Flow section of Coddy's R journey. Lesson 36 of 64.
Challenge
HardComplete sales_report(records). Each record is item:quantity:price. Build the report in three stages:
- Parse the records into a list with
lapply(). - Keep the records with a quantity above 0 with
Filter(). - Compute each kept line total (quantity times price) with
sapply()and the grand total withReduce().
Return one line per kept record, tea: 8.00, then Total: with the grand total and Skipped: with the number of records that had quantity 0. Show money with two decimals.
The supplied code reads the records as a comma-separated line and prints each returned line.
Try it yourself
sales_report <- function(records) {
# Write your code here
character(0)
}
# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
cat(sales_report(strsplit(input[1], ",")[[1]]), 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