This demo will use the iris dataset.
When selecting data, aggregated output can be returned. The most common aggregation functions are: SUM, AVERAGE, COUNT, MIN and MAX.
Example 1: What is the sum of all the petal widths?
sqldf("
SELECT SUM(`Petal.Width`) FROM iris;
")
## SUM(`Petal.Width`)
## 1 179.9
Example 2: What is the average sepal length?
sqldf("
SELECT AVG(`Sepal.Length`) FROM iris;
")
## AVG(`Sepal.Length`)
## 1 5.843333
Example 3: How many rows are in the iris dataset?
sqldf("
SELECT COUNT(Species) FROM iris
")
## COUNT(Species)
## 1 150
Example 4: What is the smallest petal length?
sqldf("
SELECT MIN(`Petal.Length`) FROM iris;
")
## MIN(`Petal.Length`)
## 1 1
Example 5: How long is the longest petal?
sqldf("
SELECT MAX(`Petal.Length`) FROM iris;
")
## MAX(`Petal.Length`)
## 1 6.9
All functions are relatively easy to implement, the difficulty is realising when you should be using each!