๐ŸŽ“ A1Learner
๐Ÿ”
0 of 40 lessons completed

R Project: Data Report

A data report turns a small table into clear answers. It builds the table first and then works through it step by step.

Build the table and add revenue

This code builds a small shop table with price and sold columns. A new revenue column comes from price times sold for each row.

shop <- data.frame(name = c("pen", "pad", "mug", "bag"), price = c(2, 4, 6, 10), sold = c(25, 20, 10, 8))
shop$revenue <- shop$price * shop$sold
print(shop)

Total and mean revenue

sum adds all the revenue values into one total. mean then shows the average revenue across the rows.

shop <- data.frame(name = c("pen", "pad", "mug", "bag"), price = c(2, 4, 6, 10), sold = c(25, 20, 10, 8))
shop$revenue <- shop$price * shop$sold
print(sum(shop$revenue))
print(mean(shop$revenue))

Rows above the line

The filter keeps only rows where revenue is above 60. The printed frame shows the pad and bag rows.

shop <- data.frame(name = c("pen", "pad", "mug", "bag"), price = c(2, 4, 6, 10), sold = c(25, 20, 10, 8))
shop$revenue <- shop$price * shop$sold
top <- shop[shop$revenue > 60, ]
print(top)

Example

shop <- data.frame(name = c("pen", "pad", "mug", "bag"), price = c(2, 4, 6, 10), sold = c(25, 20, 10, 8))
shop$revenue <- shop$price * shop$sold
print(shop)
print(sum(shop$revenue))
top <- shop[shop$revenue > 60, ]
cat("Top sellers: ", paste(top$name, collapse = ", "), "\n", sep = "")

Key points

  • The report starts with a data frame of shop items.
  • A computed column adds revenue to the frame.
  • sum() and mean() give the headline numbers.
  • A filter keeps only the rows above a chosen revenue.
  • paste() with collapse joins names into one line.
  • cat() prints the final report line.

In this example, the report prints the full table and the total revenue. It then names the items above the chosen revenue in one line.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท rdrr.io R Snippets

Exercise

How does the report add the revenue column?