R for ecologists
The counts do not arrive in R by magic. Between a notebook full of field records and a diversity index there is a spreadsheet, a read, a reshape, a round of name fixing and a join, and every one of those steps can change the number at the end without raising an error. Data tidying is not the dull part before the analysis. On real field data it is the part that decides the analysis, which is why it belongs in the same place as the statistics rather than in a footnote nobody reads.
This page collects every tutorial on this site about getting field data into R and ready to work with. All of them run in base R and the tidyverse, on small messy sheets like the ones you actually keep, and each one stops at the point where a quiet default has already changed a result.
The tidying is where the result is decided
Two of the most damaging edits in a whole analysis happen before you fit anything. The first is deciding what a blank cell means: a species that was looked for and not found is a zero, a species that was never assessed is missing, and a spreadsheet writes both the same way, so the choice of how to read that blank is an ecological claim disguised as an import setting. The second is deciding when two spellings are one species. A name written a slightly different way each day becomes several species in a richness count, and because the drift piles up fastest in the plots where you recorded the most, the inflation lands hardest exactly where you worked hardest. Neither edit announces itself. Both move the number your reviewer will ask about.
The zeros nobody wrote down
A field notebook records what was there. It has no row for what was absent, and that absent row is a real ecological zero that the analysis needs. Two ordinary operations turn on it. Reshaping a long list of records into a species-by-site table has to invent the cells for combinations the notebook never mentioned, and whether those cells become zeros or stay missing is a decision the reshape makes for you unless you make it yourself. Joining a table of sites to a table of observations does the same thing from the other side: the default join keeps only the rows that matched, which quietly deletes every site where the focal species was never seen, and a mean abundance computed on what is left is a mean over the places the species occurred, not over the places you surveyed. The zeros carry the ecology. Losing them shifts a habitat contrast without touching a single count.
A summary hides a choice; a session hides its state
Once the table is clean, most ecological questions become a grouped summary, and the one-liner that produces it buries a choice. Averaging a proportion across sites is not the same as pooling the counts and taking the proportion once; when effort differs between sites the two answer different questions, and the function call looks identical either way. The same invisibility runs under the whole session. A value left over in the workspace from an earlier run will feed into today’s code and change the result with no warning at all, which is why a reproducible analysis is a script run in a clean session rather than a stream of commands typed into a console over an afternoon. Looking at the raw table in a quick plot before you summarise it is the cheapest guard against all of this: a plot shows the outlier, the stray name and the missing site that a single number folds away.
The tutorials
The session comes first
Get a working setup in place before the data, so today’s result is still the result tomorrow.
- Setting up R and RStudio for ecology - a project instead of a hard-coded path, packages loaded every session, and the leftover value that rewrites a diversity estimate in silence.
Getting field data ready
The operations that turn a raw sheet into a table a package will accept, each with the default that changes a number.
- Reading field data into R - the import itself, and the three different things a blank cell can mean.
- Wide and long: reshaping species data - moving between a species matrix and one row per record, and the zero a notebook cannot write.
- Cleaning species names before you count - trimming and a lookup table done in the order that works, and why fuzzy matching quietly merges real species.
- Joining ecological tables without losing zeros - why the default join deletes absent sites, and the join that keeps them.
A first plot and a first summary
Turn the clean table into something you can look at and something you can report.
- Your first ggplot: data, aes and geom - the three pieces every plot is built from, and the colour-in-the-wrong-place error that costs beginners the most time.
- Summarising ecological data by group - group_by and summarise, and why averaging proportions answers a different question than pooling the counts.
The whole pipeline at once
Every step above, run end to end on one messy sheet, so you can watch the decisions compound.
- From a field sheet to your first analysis - a worked run from a raw notebook to a diversity result, where the cleaning choices decide which site comes out on top.
Where this connects
- A reproducible statistical workflow takes the clean-session habit from the setup post and turns it into a full pipeline with seeds, version pinning and relative paths.
- Standard errors and confidence intervals and bootstrap confidence intervals are the next step once the data is clean: the first honest question about any number is how uncertain it is.
- Species diversity in R is where the last tutorial hands off, since the first analysis in this series is a diversity index and that page is what the index means.
- Ordination in R starts from the same clean site-by-species table, one row per site, that this series is built to produce.