
π This article includes 2 downloadable practice files β
R is built for statistics and charts. If your Excel work involves averages, distributions or trends, R can do in a few lines what takes many clicks. Install R from CRAN and RStudio (free), then follow along.
In this article
Install the packages (once)
install.packages(c("readxl", "dplyr", "ggplot2"))
The 15 lines
library(readxl)
library(dplyr)
library(ggplot2)
sales <- read_excel("sales.xlsx", sheet = "Data")
summary_tbl <- sales |>
mutate(amount = qty * price) |>
group_by(region) |>
summarise(orders = n(),
revenue = sum(amount),
avg_order = mean(amount)) |>
arrange(desc(revenue))
print(summary_tbl)
ggplot(summary_tbl, aes(x = reorder(region, revenue), y = revenue)) +
geom_col(fill = "#e47f53") + coord_flip() +
labs(x = NULL, y = "Revenue", title = "Revenue by region")
Reading it like Excel
| R | Excel equivalent |
|---|---|
mutate(amount = qty * price) |
Add a formula column |
group_by(region) + summarise() |
Pivot table with region in Rows |
arrange(desc(revenue)) |
Sort largest to smallest |
ggplot(...) + geom_col() |
Insert β Bar chart |
The |> symbol (the βpipeβ) passes the result of one step into the next. Read it as βand thenβ.
π‘ Where R really shines is the next step:
summary(sales$amount) gives min, quartiles, median and mean in one line, and geom_histogram() shows the distribution β handy for spotting outliers before month-end.Try it yourself: step by step
- Install R and RStudio (both free). Download sales.xlsx and first_summary.R into one folder.
- Open first_summary.R in RStudio, then Session β Set Working Directory β To Source File Location.
- Run
install.packages(c("readxl","dplyr","ggplot2"))once in the Console. - Click Source. The summary table prints in the Console and the bar chart appears in the Plots pane.
- Try
summary(sales$Qty)in the Console for min, quartiles, median and mean in one line.
π Practice files for this article
- πSales workbookData sheet used by the R script.β¬ XLSX Β· 7 KB
- πR scriptOpen in RStudio next to sales.xlsx and click Source.β¬ R Β· 529 B
Free to use for learning. Files with macros (.bas) are plain text β import them with Alt+F11 β File β Import File, and always test on a copy.