R for ecologists

R for ecologists: read a field sheet into R, clean species names, reshape and join tables, and reach a first diversity result, with what each step decides.

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.

A first plot and a first summary

Turn the clean table into something you can look at and something you can report.

The whole pipeline at once

Every step above, run end to end on one messy sheet, so you can watch the decisions compound.

Where this connects

Newsletter

Get new tutorials by email

New R and QGIS tutorials for ecologists, straight to your inbox. No spam; unsubscribe anytime.

By subscribing you agree to receive these emails and confirm your address once. See the privacy policy.