Menu
Coddy logo textTech

Recap - Sales Report

Part of the Logic & Flow section of Coddy's R journey. Lesson 46 of 64.

challenge icon

Challenge

Medium

Complete sales_report(df). df has the columns region, product, units and price. Build the report:

  1. Add a revenue column: units times price.
  2. Keep the rows with at least one unit sold.
  3. Return one line per region in alphabetical order, north: 45.00, with the region's total revenue.
  4. Add Top product: followed by the product with the highest revenue in a single row, and Rows 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

Practice on your own: Online R compiler