Project: The Plant Experiment Report
Build a complete R script that computes growth per plant, summarises each fertiliser group with means and standard deviations, ranks the plants, and prints a formatted report that is checked against Python.
In this lesson you will
- Combine data frames, functions and grouping
- Compute growth and percentage gain
- Format a report with sprintf
- Verify every figure independently
About 28 minutes
From twelve measurements to a conclusion
The first project puts the lessons together. Nia measured every plant twice, in week 1 and in week 4, and she wants a report that answers: how much did each group grow, which plants grew most, and how large is the difference between the groups compared with the variation inside a group. The script builds a data frame, adds a growth column and a percentage gain column, summarises by group with aggregate, uses sprintf to print aligned lines, and ranks the plants with order.
Growth is the week-4 height minus the week-1 height, and the percentage gain is growth divided by the week-1 height, times 100. The report uses three statistics per group: the mean growth, the sample standard deviation to show the spread, and the number of plants. The function sprintf formats numbers with C-style codes, %d for an integer, %.2f for two decimals, %5.1f for a width of five with one decimal, and %s for text, which gives tidy columns that cat alone cannot.
plants <- data.frame(
id = 1:12,
group = factor(rep(c("control", "organic", "chemical"), each = 4), levels = c("control", "organic", "chemical")),
week1 = c(4.2, 4.8, 3.9, 4.5, 4.6, 5.1, 4.4, 4.9, 4.7, 4.3, 5.0, 4.8),
week4 = c(12.1, 13.4, 11.8, 12.9, 15.2, 16.8, 14.9, 15.7, 18.3, 17.5, 19.1, 18.8)
)
plants$growth <- round(plants$week4 - plants$week1, 2)
plants$gain_pct <- round(plants$growth / plants$week1 * 100, 1)
summ <- aggregate(growth ~ group, data = plants, FUN = function(x) c(n = length(x), mean = mean(x), sd = sd(x)))
cat(sprintf("%-10s %3s %8s %8s\n", "group", "n", "mean", "sd"))
for (i in seq_len(nrow(summ))) {
m <- summ$growth[i, ]
cat(sprintf("%-10s %3d %8.3f %8.3f\n", summ$group[i], as.integer(m["n"]), m["mean"], m["sd"]))
}
best <- plants[order(-plants$growth), ][1:3, ]
cat("\nTop three by growth:\n")
for (i in seq_len(nrow(best))) cat(sprintf(" plant %2d (%s): +%.2f cm, %.1f%%\n", best$id[i], best$group[i], best$growth[i], best$gain_pct[i]))
ctrl <- mean(plants$growth[plants$group == "control"])
for (g in c("organic", "chemical")) {
gm <- mean(plants$growth[plants$group == g])
cat(sprintf("%s grew %.3f cm more than control (%.1f%% more)\n", g, gm - ctrl, (gm - ctrl) / ctrl * 100))
}
cat(sprintf("overall: mean growth %.3f, smallest %.2f (plant %d), largest %.2f (plant %d)\n", mean(plants$growth), min(plants$growth), plants$id[which.min(plants$growth)], max(plants$growth), plants$id[which.max(plants$growth)]))
group n mean sd
control 4 8.200 0.356
organic 4 10.900 0.548
chemical 4 13.725 0.411
Top three by growth:
plant 11 (chemical): +14.10 cm, 282.0%
plant 12 (chemical): +14.00 cm, 291.7%
plant 9 (chemical): +13.60 cm, 289.4%
organic grew 2.700 cm more than control (32.9% more)
chemical grew 5.525 cm more than control (67.4% more)
overall: mean growth 10.942, smallest 7.90 (plant 1), largest 14.10 (plant 11)
The report printed one row per group with the number of plants, the mean growth and the standard deviation: control has 4 plants, mean 8.200 cm, sd 0.356; organic has 4 plants, mean 10.900 cm, sd 0.548; chemical has 4 plants, mean 13.725 cm, sd 0.411. The standard deviations are small compared with the differences between the means, which is the first sign that the groups really differ, and lesson 9 tests that formally.
The ranking by growth shows the three plants that grew most: plant 11 in the chemical group grew 14.10 cm, a gain of 282.0 percent; plant 12 in the chemical group grew 14.00 cm, a gain of 291.7 percent; plant 9 in the chemical group grew 13.60 cm, a gain of 289.4 percent. Compared with the control mean growth of 8.200 cm, the organic group grew 2.700 cm more, which is 32.9 percent more and the chemical group grew 5.525 cm more, which is 67.4 percent more. Over all twelve plants the mean growth is 10.942 cm, the smallest growth is 7.90 cm (plant 1) and the largest is 14.10 cm (plant 11).
Independent check: Python recomputed growth, percentage gain and the three group summaries from the same 24 numbers with the statistics module, ranked the plants with sorted, and every figure printed above matches, including the percentages and the plant numbers. Matching formatting too: the %-10s code left-aligns the group names in a field of ten, and %8.3f right-aligns a number in eight columns with three decimals, as the output block shows.
Check by hand with plant 1: it grew 12.1 minus 4.2, which is 7.9 cm, and 7.9 divided by 4.2 is 1.881, so a gain of 188.1 percent. For the control group the four growths are 7.9, 8.6, 7.9 and 8.4, whose mean is 32.8 / 4 = 8.2.
A difference of means means little without knowing how much plants within a group vary. Always print the standard deviation or a range next to the mean, and the number of observations.
Because the whole report comes from one script, next year’s class can change the data and rerun it. Put the data in a file, as in lesson 6, and the script stays unchanged.
Every program in this lesson was run with R 4.3.3 using Rscript –vanilla (base R only, no packages) while the course was written. R releases new versions every year, so check r-project.org for the current version and for any change in behaviour. The week-1 and week-4 heights are the classroom data of this course, entered by hand in the script.
Practice
Add a column for the leaf count and report the mean leaves per group. Print the plant with the lowest growth in each group using split and lapply. Change sprintf so that the report also shows the minimum and the maximum growth per group.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
How is growth computed here?
- week1 minus week4
- The mean of both
- Their ratio
- week4 minus week1
Percentage gain divides it by week1.
What does sprintf(“%8.3f”, x) do?
- Prints x as integer
- Rounds to 8 digits
- Formats x in 8 columns with 3 decimals
- Converts to text only
Useful for aligned columns.
Why print the standard deviation?
- It is required
- It replaces the mean
- It shows how much plants vary within a group
- It sorts the data
Differences need context.
What does order(-x) do?
- Sorts ascending
- Gives positions sorting from largest
- Deletes x
- Returns the maximum
Used to rank plants.
How was the report checked?
- Not checked
- Recomputed independently in Python
- By eye
- With a graph
Every figure was compared.
Summary
- Compute derived columns, summarise by group, rank and print.
- sprintf gives aligned, formatted output.
- Report the spread and the number of observations with every mean.
- Cross-check the numbers with an independent implementation.
Questions
Is aggregate with a function returning a vector normal?
Yes, but the result is a matrix inside a data frame column, which is why the code uses summ$growth[i, ]. For simpler results, use one function per call.
How do I round to a given number of decimals?
round(x, 2) rounds the value, and sprintf("%.2f", x) formats it as text. Use round for calculations and sprintf for display.
Why percentage gain relative to week 1?
It makes plants of different starting size comparable. Absolute growth in centimetres is also reported, since each answers a different question.
